Android GestureDetector cannot detect onScroll event using FrameLayout

I have a view that extends FrameLayout and should be notified of scroll events on it. this view has an instance of a class that implements the GestureDetector, which is called 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 can get a wheen call that I am trying to scroll through, but the actual onScroll methods are never called.

  @Override public boolean onInterceptTouchEvent(MotionEvent event) { boolean result = super.onInterceptTouchEvent(event); if (gestureDetector.onTouchEvent(event)) { return result; } else { return false; } } 
+4
source share
1 answer

onInterceptTouchEvent is not called again for the motion sequence after returning true . Events are dispatched to onTouchEvent immediately afterwards (since they are now intercepted from children).

Here you need two changes:

  • OnGestureListener.onDown() must return true so that the detector can handle more complex gestures, such as scrolls
  • onInterceptTouchEvent should always return false so that the stream of threads coming into this method
0
source

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


All Articles