adding touch listener for liner layout filled with buttons

前端 未结 2 1669
遇见更好的自我
遇见更好的自我 2020-12-09 06:46

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

相关标签:
2条回答
  • 2020-12-09 07:35

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-09 07:49

    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

    0 讨论(0)
提交回复
热议问题