5

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; } } 
3
  • Did you try to return true from onDown()? Commented Feb 10, 2012 at 15:53
  • Yes, But it it never reaches to onScroll(..) Commented Feb 10, 2012 at 16:22
  • Sorry I cannot give you an answer but according to my tests onInterceptTouchEvent does not receive all touch events but only the initial down event. Therefore the GestureDetector cannot do its work. (Try adding debug output to the method.) According to the official documentation one has to return false to receive all following events but this does not seem to be true. Commented Feb 11, 2012 at 0:25

1 Answer 1

2

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 scrolls
  • onInterceptTouchEvent should always return false to keep the stream of events flowing to this method
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.