Android - onScroll Gesture Detector

I want to implement onScroll when a scroll event occurs. However, I don’t understand how I can determine the bottom-up scrolling with the parameters that I get with onScroll (MotionEvent e1, MotionEvent e2, float distanceX, floating distance Y).

I would be glad to receive some recommendations on its implementation or some examples.

+4
source share
1 answer

You should be able to use the parameter distanceYto determine if the view has scrolled up or down. distanceYrepresents the distance along the Y axis that has been scrolled since the last call onScroll(). If the value is distanceYgreater than zero, the view scrolls from a lower position on the Y axis to a higher position on the Y axis.

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    if (distanceY > 0) {
        // Scrolled upward
        if (e2.getAction() = MotionEvent.ACTION_UP) {
            // The pointer has gone up, ending the gesture
        }
    }
    return false;
}

Note. I have not tested if it MotionEvent.ACTION_UPdecides your need to check if the scrolling is over, but this seems practical in theory. Also note that technically this gesture can also end if the parameter is MotionEventset to MotionEvent.ACTION_CANCEL.

+3
source

All Articles