Adding a touch listener for the layout of the liner filled with buttons

I added a touch event to the linear layout to respond to finger gestures, and it works well. However, when I add buttons to the layout, the layout of the parent layout is ignored. How to prevent this?

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

How to use onInterceptTouch ?

+4
source share
2 answers

You must create your own layout and override the yourInterceptTouchEvent (MotionEvent ev) method of your layout.

As an example, I created my own layout that 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:%sy:%s", break; case MotionEvent.ACTION_MOVE: //Log.d(TAG, String.format("ACTION_MOVE | x:%sy:%s", break; case MotionEvent.ACTION_UP: break; } return true; } 

And when I clicked a button on my layout, even I clicked on this button, My Layout consumes all touchEvent, since onInterceptTouchEvent always returns true.

Hope this helps you

+10
source

Add android: onClick = "tapEvent" in your layout that you want to click. By changing the value of MAX_TAP_COUNT, 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
source

Source: https://habr.com/ru/post/1410873/


All Articles