I have a view that extends FrameLayout and need to be notified of the scrolling events on it. this view has an instance of a class that implements the GestureDetector which is invoked by the overriden onInterceptTouchEvent method.
private class HorizontalScrollListener implements OnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
...
return false;
}
@Override
public boolean onDown(MotionEvent e) {
...
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
...
System.out.println();
}
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onSingleTapUp(MotionEvent e) { return false; }
}
The only problem is that the onDown and onLongPress methods could get called wheen I try to scroll but the actual onScroll methods never gets invoked.
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean result = super.onInterceptTouchEvent(event);
if (gestureDetector.onTouchEvent(event)) {
return result;
} else {
return false;
}
}
onInterceptTouchEvent
is not called again for a motion sequence once it returns true
. Events are sent to onTouchEvent
directly afterwards (since they are now being intercepted from the children).
You need two changes here:
OnGestureListener.onDown()
should return true
so the detector can process more complex gestures like scrollsonInterceptTouchEvent
should always return false
to keep the stream of events flowing to this methodIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With