gustav12
gustav12

Reputation: 327

Prevent recyclerview from stop scrolling on touch event

I have a simple recyclerview with an adapter of strings displaying a list of numbers, i want to avoid the recyclerview to stop when it is scrolling and it's touched by the user. I've already canceled to manually scroll the recyclerview on dragging by the user, but i need to manage the scrolling by myself programmatically (start and stop scroll)

I dont want to let the user interact with the recyclerview. Here's a small video showing what happens when the user touches the recyclerview when its scrolling (it stops). I want to cancel that behaviour.

this is how i cancel scroll on dragging by the user

recyclerViewSlot.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            // set return "true" = disables scrolling behaviour on dragging
            return true;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }
    });

by adding these lines of code, there is no change on the behaviour.

recyclerViewSlot.setEnabled(false);
recyclerViewSlot.setClickable(false);

Upvotes: 3

Views: 3289

Answers (2)

Borja
Borja

Reputation: 1318

Overriding onInterceptTouchEvent in a custom RecyclerView you can achieve that.

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    return false;
}

Upvotes: 1

Niza Siwale
Niza Siwale

Reputation: 2404

You have to extend the RecyclerView class then manually prevent onTouch from being called like so:

public class YourRecyclerView extends RecyclerView {

    private boolean lock=false;
    public YourRecyclerView (Context context) {
        super(context);

    }

    public YourRecyclerView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YourRecyclerView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(!lock){

        return super.onTouchEvent(ev);
        }
        else{
            return true;
        }

    }


    /**
     * @return the lock
     */
    public boolean isLock() {
        return lock;
    }

    /**
     * @param lock the lock to set
     */
    public void setLock(boolean lock) {
        this.lock = lock;
    }
}

Upvotes: 1

Related Questions