Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical Recyclerview only allow scrolling down but block scrolling up

How can I limit the scrolling ability of a vertical Recyclerview to only allow scrolling down?

I want to make somehow a "list with no return to the top".

EDIT: It's not a duplicate. I don't want to disable scrolling vertically. I just want to disable scrolling upwards.

like image 375
Stefan Sprenger Avatar asked Dec 06 '25 20:12

Stefan Sprenger


1 Answers

I figured out a solution using an OnItemTouchListener.

The scrolling event consists of 3 MotionEvents : ACTION_DOWN , ACTION_MOVE and ACTION_UP.

So on ACTION_DOWN we get the vertical position of the cursor (Y) and the on ACTION_MOVE we compare the new position to the old one.

By returning true, the method onInterceptTouchEvent() makes sure we intercept the scrolling event.

float lastY;

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
        int action = event.getAction();
        if(action == MotionEvent.ACTION_DOWN) {
            lastY = event.getY();
        }
        if (action == MotionEvent.ACTION_MOVE && event.getY() > lastY) {
            return true;
        }
        return false;
    }
    ...
like image 81
Hristo Stoyanov Avatar answered Dec 08 '25 09:12

Hristo Stoyanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!