Stefan Sprenger
Stefan Sprenger

Reputation: 1080

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.

Upvotes: 3

Views: 882

Answers (1)

Hristo Stoyanov
Hristo Stoyanov

Reputation: 1960

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

Upvotes: 2

Related Questions