Android 如何在视图的特定方面激活onTouchEvent()运动?
例如,我的一个XML文件中有一个scrollView和一个LinearLayout。LinearLayout是屏幕底部的一个小条,用作导航(70dp高) 我想这样做,如果用户在该线性布局中向上/向下滑动,它将在其下方显示另一个线性布局。有点像一个扩展菜单 我已经创建并放置了扩展布局,动画正在运行。但我无法让它在线性布局内上下滑动 我该怎么做?提前谢谢 编辑:代码示例 这是XML的一个示例Android 如何在视图的特定方面激活onTouchEvent()运动?,android,layout,Android,Layout,例如,我的一个XML文件中有一个scrollView和一个LinearLayout。LinearLayout是屏幕底部的一个小条,用作导航(70dp高) 我想这样做,如果用户在该线性布局中向上/向下滑动,它将在其下方显示另一个线性布局。有点像一个扩展菜单 我已经创建并放置了扩展布局,动画正在运行。但我无法让它在线性布局内上下滑动 我该怎么做?提前谢谢 编辑:代码示例 这是XML的一个示例 <WebView xmlns:android="http://schemas.android.com
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/llSM"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/llSM"
android:orientation="horizontal"
android:layout_above="@+id/llNav"
android:layout_alignWithParentIfMissing="true"
>
<ImageButton
android:id="@+id/bWebsite"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/nhome"
android:clickable="true"
android:scaleType="fitXY"
android:background="#FFFFFF"
android:layout_weight="1" />
<ImageButton
android:id="@+id/bShop"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/nhome"
android:clickable="true"
android:scaleType="fitXY"
android:background="#FFFFFF"
android:layout_weight="1" />
<ImageButton
android:id="@+id/bFacebook"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/nfacebook"
android:clickable="true"
android:scaleType="fitXY"
android:background="#FFFFFF"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/llNav"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_centerInParent="true"
android:gravity="bottom|center_horizontal"
>
<ImageButton
android:id="@+id/bHome"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/nhome"
android:clickable="true"
android:scaleType="fitXY"
android:background="#FFFFFF"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/bContact"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ncontactus"
android:clickable="true"
android:layout_toRightOf="@+id/bHome"
android:background="#FFFFFF"
android:scaleType="fitXY"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/bBooking"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/onlinebooking"
android:clickable="true"
android:layout_toRightOf="@+id/bContact"
android:background="#FFFFFF"
android:scaleType="fitXY"
android:layout_weight="1"
/>
</LinearLayout>
这是一个主要的例子:
protected void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_external);
ll = (LinearLayout) findViewById(R.id.llNav);
ll.setVisibility(View.GONE);
animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);
NavBar(); //This just loads the buttons and their intents
Links(); //This is for the second linear layout loading social media
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.loadUrl("url");
ll2 = (LinearLayout) findViewById(R.id.llSM);
ll2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
startY = event.getY();
//hello you just touched me!!!
} else if(event.getAction() == MotionEvent.ACTION_UP) {
float endY = event.getY();
if ((endY < startY) && (a==0)) {
ll.setVisibility(View.VISIBLE);
ll.startAnimation(animUp);
a = a+1;
}
else {
if ((endY > startY) && (a>=1)){
ll.startAnimation(animDown);
ll.setVisibility(View.GONE);
a = 0;
}
}
}
return false;
}
});
}
创建时受保护的void(Bundle savedInstanceState){
//删除标题栏
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_外部);
ll=(线性布局)findViewById(R.id.llNav);
ll.setVisibility(View.GONE);
animUp=AnimationUtils.loadAnimation(this,R.anim.anim\u up);
animtown=AnimationUtils.loadAnimation(this,R.anim.anim\u down);
NavBar();//这只是加载按钮及其意图
Links();//这是加载社交媒体的第二个线性布局
setWebViewClient(新的WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD\u NO\u缓存);
myWebView.loadUrl(“url”);
ll2=(线性布局)findViewById(R.id.llSM);
ll2.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
startY=event.getY();
//你好,你刚刚碰了我!!!
}else if(event.getAction()==MotionEvent.ACTION\u UP){
float endY=event.getY();
如果((endYstartY)和&(a>=1)){
ll.startAnimation(animDown);
ll.setVisibility(View.GONE);
a=0;
}
}
}
返回false;
}
});
}
WebView在其中滚动,因此无法在其中工作。我希望能够在标有“llSM”的线性布局上滑动,以打开其下方的“llNav”栏,并将所有内容向上推。我还需要单击“llSM”栏上的按钮以在不同的web视图之间导航。您所说的
线性布局是一个视图
+组,因此在其上调用视图。ontouch Listener
thatParticularLinearLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//hello you just touched me!!!
return true;
}
return false;
}
});
编辑1
您的代码很棒,先生,但是android中有一个叫做OnInterceptouchevent()
的东西非常适合您的情况,就像我说的linearLayout-(llSM和llNav)是一个视图组,所以您可以调用OnInterceptouchevent()
让它在孩子们看到之前拦截所有触摸事件。。维护您的触摸事件代码
好的,在这里,他们甚至有代码示例来支持您
编辑2
好的,根据您的需要尝试扩展ViewFlipper
我喜欢它,它很棒。或者viewmanimator
它们将使您的工作变得轻松。。让我们看看
public class Noodles extends ViewFlipper { // for your situation i will say extend this.. i am not saying this is
// the only way but that's the best and easiest i can think of..
// this viewgroup will be in a viewgroup maybe linear layout.. but those child linearlayout
// declare them like in different layouts okay..
Context context;// context to use
boolean intercept; // i dont know if i will use this but to indicate if it will intercept
View[] children;
float inx,iny = (float) 0.000; // this is the touched position that will be captured in on down in your touch
// i am not a OMG mathmatician but if you equate it to 0, its allways going to fail.. so the number of infinity
// depends on you.d thats just btw, so try it and see maybe i might be wrong
public Noodles(Context context,View[] children/* or LinearLayout[] children*/) { // construtor.. with the child layouts
super(context);
this.context = context;
this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // setting params..
this.setInAnimation(AnimationUtils.loadAnimation(context, someSickAnimationThatwILLSlidInFromTop)); // the animation should follow the pattern like this "R.anim.abc_slide_in_top" this is a default in animation
/// but the spelling might differ
this.setOutAnimation(AnimationUtils.loadAnimation(context, someSickAnimationThatwILLSlidOutFromTop));
this.children = children;
for(int i=0;i<children.length;i++){
this.addView(children[i]);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// here you can use math catch the movement, something like 0,0 [x,y];
// get the point loaction at with it was touched
// when y increases it is coming down, when it decresse it is going up - all in respect to the intial
// point.. you can intercept when y increase by a set of marging lets say like 20..or 10, because
// eventually x will increase as is it is not straight line..but i can not rilly say, because i am conjuring from how it worksk,maybe im right maybe i am not
if(ev.getAction() == MotionEvent.ACTION_DOWN){
Log.v("Noodles", "triggered");
intercept = false;
}
if(ev.getAction() == MotionEvent.ACTION_MOVE){
Log.v("Noodles", "noodles intercept fired");
intercept = true;
return true;
}
if(ev.getAction() == MotionEvent.ACTION_UP){
if(intercept) // yes i do
return true;
}
Log.v("Noodles", "noodles intercept ignored");
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// i think you can do the calculation
// to know if its going up or down respectively
// use this.showNext(); for showing the next item from bottom to top the this.showPrevious vice versa
Log.v("Noodles", "noodles touch fired");
//i am trying the calculation, use and see if it works
float inx,iny = 0;
if(ev.getAction() == MotionEvent.ACTION_DOWN){
Log.v("touchy", "triggered");
iny = ev.getY();
inx = ev.getX();
Log.v("touchy", "x : " +String.valueOf(inx));
Log.v("touchy", "y : " +String.valueOf(iny));
}
if(ev.getAction() == MotionEvent.ACTION_MOVE){
int retval = Float.compare(iny, ev.getY());
if(retval > 0){
//noodles i am goin up
// this flips the current view out with upward movement animation you declared the in
Log.v("touchy", "i am going up");
}else if(retval < 0){
// noodles i am goin down
// this flips the current view out with downward movement
Log.v("touchy", "i am going down");
}
// you can put your animations in their respective statements and rememba to call this.showNext(); in the upward movement and this.ShowPrevious(); the other
iny = ev.getY();
Log.v("touchy", "y : " +String.valueOf(iny));
return true;
}
if(ev.getAction() == MotionEvent.ACTION_UP){
// finger is up..
//return true; //this goes agains the logic but i am thinking to prevent the return false argument, depends on how
// you playing..
}
}
如果您复制并粘贴此内容,您可能会看到一些不寻常的错误,请更正它们,好吧,因为它们非常丰满,我想当我使用笔记本电脑时,我会更正它们以备将来使用
如果有帮助,请告诉我。我只是尝试实现此功能,但没有成功:(可能是因为linearlayout中有图像按钮吗?我将用代码编辑上面的帖子。添加了代码。我可能做了所有错事,因为我是Android新手。谢谢你的帮助!哦,好的,那么llsm和llnav都将收到此刷击,对吗?好的,请稍候up@Noodelzi已经编辑了它,并且包含了有代码帮助的链接你的案例如果你仍然觉得无聊,那么当我很空闲的时候,我会做代码@Noodelz,抱歉耽搁了,先生,事情发生了这么多!我会试试看,看看我会怎么做:)如果我不能让它工作,那么我可能会回来哈哈。你自己尝试一下+1
// in your actvity instatiate it
Noodles n = new Noodles(this,View1,View2);
// then add it to your parent view or your layout that is to be your navigation