如何在视图的特定方面激活onTouchEvent()运动?

时间:2015-02-02 23:06:39

标签: android layout

例如,我的一个XML文件中有一个scrollView和一个LinearLayout。 LinearLayout是屏幕底部的一个小条,用作导航(70dp高)。

我想这样做,如果用户在此LinearLayout中向上/向下滑动,它将在其下方显示另一个LinearLayout。有点像扩展菜单。

我创建并放置了扩展布局,动画正在运行。但我无法在LinearLayout中向上/向下滑动。

我该怎么做?提前谢谢!

编辑:代码示例

这是XML的一个例子

<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;
        }
    });

}

WebView在其中滚动,因此无法在那里工作。我希望能够在标记为“llSM”的线性布局上滑动以调出其下方的“llNav”条并将所有内容都向上推。我还需要单击“llSM”栏上的按钮在不同的Web视图之间导航。

1 个答案:

答案 0 :(得分:0)

您所谈论的LinearLayoutView +群组,因此请致电View.OnTouchListener

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中有一些名为onInterceptTouchEvent()的东西,对你的情况来说是完美的,就像我说的linearLayout - ( llSM llNav )是ViewGroup,因此您可以致电onInterceptTouchEvent(),以便在孩子们看到之前拦截所有触摸事件。维护您的触摸事件代码..

好的,read more在这里,他们甚至有代码示例来支持你.. and moreand more ..

编辑2 好吧,从你想要的东西尝试扩展ViewFlipper我喜欢它,它的伟大。或ViewAnimator他们会让你的工作变得轻松..让我们看看,

   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.. 
    }


}

}

将您的MainActivity设置为此Noodles

// 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

请你复制并粘贴它,你可能会看到一些不寻常的错误,只是纠正它们,因为它们非常胖乎我想 当我到我的笔记本电脑时我会纠正它们供将来使用

让我知道它是否有帮助..