Android View stops receiving touch events when parent scrolls

I have a custom Android view that overrides onTouchEvent (MotionEvent) to handle horizontal scrolling of content in a view. However, when the ScrollView in which it is contained scrolls vertically, the user view stops receiving touch events. Ideally, I want the custom view to continue to receive events so that it can handle its own horizontal scrolling, while the containing hierarchy of views deals with vertical scrolling.

Is there a way to continue receiving these motion events while scrolling? If not, is there any other way to get the touch events that I need?

+4
source share
2 answers

I answer my question, in case someone else would be so bad at googling for an answer as I apparently was .: P

The workaround for this problem is to extend the ScrollView and override the onInterceptTouchEvent method so that it only catches touch events when the Y movement is significant (more than the X movement, according to one suggestion).

+1
source

Use requestDisallowInterceptTouchEvent (true) in childview to prevent vertical scrolling if you want to continue horizontal scrolling and the last reset when it is done.

private float downXpos = 0; private float downYpos = 0; private boolean touchcaptured = false; @Override public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: downXpos = event.getX(); downYpos = event.getY(); touchcaptured = false; break; case MotionEvent.ACTION_UP: requestDisallowInterceptTouchEvent(false); break; case MotionEvent.ACTION_MOVE: float xdisplacement = Math.abs(event.getX() - downXpos); float ydisplacement = Math.abs(event.getY() - downYpos); if( !touchcaptured && xdisplacement > ydisplacement && xdisplacement > 10) { requestDisallowInterceptTouchEvent(true); touchcaptured = true; } break; } super.onTouchEvent(event); return true; } 
+6
source

All Articles