How to use gestures with scrollview - android?

I am using onFling in my views to move to the next activity. However, this does not work if the view has a scrollview surrounding it.

It seems to be absorbing gestures and stopping my onFling.

Anyway, around this problem?

0


source to share


3 answers


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); 
    }
}

      

+2


source


You are correct, the ScrollView "steals" the gesture because it inherits functionality. I've already worked around this by applying the onTouchListener to the ScrollView itself, not its immediate parent view.



+4


source


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.

0


source







All Articles