adding touch listener for liner layout filled with

2019-01-17 18:59发布

I added a touch event to a linear layout to respond to swipe gestures, and it works well. However, when i add buttons to the layout, the parent liner layout is ignored. How should I prevent this from happening?

LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);

How to i use onInterceptTouch?

2条回答
手持菜刀,她持情操
2楼-- · 2019-01-17 19:13

You should create your own layout and override onInterceptTouchEvent(MotionEvent ev) method of your layout.

For Example I created my own layout which extends RelativeLayout

       @Override
       public boolean onInterceptTouchEvent(MotionEvent ev) {
         return true; // With this i tell my layout to consume all the touch events from its childs
      }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        // Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s", 
            break;
        case MotionEvent.ACTION_MOVE:
        //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
            break;
        case MotionEvent.ACTION_UP:
            break;
        }
        return true;
    }

And when i put a button to my layout, even i clicked that button, My Layout consumes all the touchEvent because of the onInterceptTouchEvent always returns true.

Hope this should help you

查看更多
狗以群分
3楼-- · 2019-01-17 19:34

Add android:onClick="tapEvent" in your layout which you want to take the click. By changing the MAX_TAP_COUNT value, you can use any number of taps.

private long thisTime = 0;
private long prevTime = 0;
private int tapCount = 0;
private static  final int MAX_TAP_COUNT = 5;
protected static final long DOUBLE_CLICK_MAX_DELAY = 500;


public void tapEvent(View v){

        if (SystemClock.uptimeMillis() > thisTime) {
            if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
                Log.d(TAG, "touch event " + "resetting tapCount = 0");
                tapCount = 0;
            }
            if (tapCount()) {
               //DO YOUR LOGIC HERE
            }
        }

}

private Boolean tapCount(){

        if (tapCount == 0) {
            thisTime = SystemClock.uptimeMillis();
            tapCount++;
        } else if (tapCount < (MAX_TAP_COUNT-1)) {
            tapCount++;
        } else {
            prevTime = thisTime;
            thisTime = SystemClock.uptimeMillis();

            //just incase system clock reset to zero
            if (thisTime > prevTime) {

                //Check if times are within our max delay
                if ((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
                    //We have detected a multiple continuous tap!
                    //Once receive multiple tap, reset tap count to zero for consider next tap as new start
                    tapCount = 0;
                    return true;
                } else {
                    //Otherwise Reset tapCount
                    tapCount = 0;
                }
            } else {
                tapCount = 0;
            }
        }
        return false;

}
查看更多
登录 后发表回答