adding touch listener for liner layout filled with buttons

前端 未结 2 1744
遇见更好的自我
遇见更好的自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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;
    
    }
    

提交回复
热议问题