How to use gestures with scrollview - android?
Check out this piece of code: (Override ScrollView
dispatchTouchEvent)
public class yourScrollView extends ScrollView{
//constructors and everything
//You might want to pass your GestureDetector (of course)
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return myGestureDetector.onTouchEvent(ev);
}
}
source to share
I cannot comment on the answers, so I am posting a new one. I found that overriding dispatchTouchEvent
of ScrollView
works well, but the gesture handler needs to be called before super.dispatchTouchEvent
, as this method can change the coordinates of events in some strange way. In particular, I have seen the Y value jump when trying to scroll vertically past the end of the view. Calling the gesture handler before processing the scroll view will allow the scroll coordinates to be used rather than the internal scrolled ones.
So:
public class yourScrollView extends ScrollView{
//constructors and everything
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
return myGestureDetector.onTouchEvent(ev) | super.dispatchTouchEvent(ev);
}
}
Items in the scroll view are responsive until the view triggers scrolling, but the gestures are recognized correctly.
source to share