Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - onScroll Gesture Detector

I want to implement onScroll, when scroll event occurs. However I don't understand how can I detect the scroll from bottom up with the parameters that I receive with onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY).

I would be happy to get some guidelines how to implement it or some examples.

like image 571
Vera Avatar asked Oct 19 '22 10:10

Vera


1 Answers

You should be able to use the distanceY parameter to determine whether the view was scrolled up or down. distanceY represents the distance along the Y axis that has been scrolled since the last call to onScroll(). If the value of distanceY is greater than zero, the view has been scrolled 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 the MotionEvent.ACTION_UP will solve your need to check when scrolling has ended, but it seems practical in theory. Also note that technically the gesture could also end if the MotionEvent's action is set to MotionEvent.ACTION_CANCEL.

like image 62
Ryan Avatar answered Oct 21 '22 04:10

Ryan