Skip to content Skip to sidebar Skip to footer

How To Use Gestures With Scrollview - Android?

I am using onFling in my views to get a swipe to next activity. However this does not work if the view has a scrollview surrounding it. Apparently it absorbs the gestures and stop

Solution 1:

You are right, ScrollView "steals" the gesture because of it's inherit functionality. I've worked around this before by applying the onTouchListener to the ScrollView itself instead of its immediate parent view.

Solution 2:

Check this piece of code: (Override ScrollView's dispatchTouchEvent)

publicclassyourScrollViewextendsScrollView{

    //constructors and everything//You might want to pass your GestureDetector (of course)@OverridepublicbooleandispatchTouchEvent(MotionEvent ev){
        super.dispatchTouchEvent(ev);    
        return myGestureDetector.onTouchEvent(ev); 
    }
}

Solution 3:

I can't comment answers so I write a new one. I found that overriding the dispatchTouchEvent of ScrollView works well but the gesture handler needs to be called before the super.dispatchTouchEvent as that method could change the event coordinates in some weird way. In particular I have seen the Y value jumping when trying to vertically scroll past the end of the view. Calling the gesture handler before the scroll view handling will let it use the scroll view coordinates and not the internal scrolled ones.

So:

publicclassyourScrollViewextendsScrollView{

    //constructors and everything@OverridepublicbooleandispatchTouchEvent(MotionEvent ev){
        return myGestureDetector.onTouchEvent(ev) | super.dispatchTouchEvent(ev); 
    }
}

Elements in the scroll view react as long as the view does not start scrolling, but the gestures are correctly detected.

Post a Comment for "How To Use Gestures With Scrollview - Android?"