user3606902
user3606902

Reputation: 819

How to disable manual scroll of Recyclerview?

I have horizontal Recyclerview and want to disable manual scroll of it. But on click of item it should scroll. How to do it?

Upvotes: 2

Views: 4421

Answers (4)

Md. Arif
Md. Arif

Reputation: 508

Thanks to Emi Raz. His answer is so simple for disabling scroll behaviour on recyclerview. And the solution works for me. please see his solution here

java:

LinearLayoutManager lm = new LinearLayoutManager(getContext()) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

kotlin:

val lm: LinearLayoutManager = object : LinearLayoutManager(requireContext()) {
    override fun canScrollVertically(): Boolean { return false }
}

Upvotes: 1

droidev
droidev

Reputation: 7380

you have to create a custom layout manager for this,you can disable the scrolling in this way

example:

public class CustomLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;

public CustomLayoutManager(Context context) {
    super(context);
}

public void setScrollEnabled(boolean flag) {
    this.isScrollEnabled = flag;
}

@Override
public boolean canScrollHorizontally() {
   //Similarly you can customize "canScrollVertically()" for managing horizontal scroll
    return isScrollEnabled && super.canScrollHorizontally();
}

this way you can disable manual scrolling

Upvotes: 2

Paresh
Paresh

Reputation: 6857

// You can set `onTouchListener`

public class RecyclerViewTouch implements RecyclerView.OnItemTouchListener {

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return true;
    }

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

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

// Use it
RecyclerView.OnItemTouchListener disable = new RecyclerViewTouch();
rView.addOnItemTouchListener(disable);        // disables scolling
rView.removeOnItemTouchListener(disable);     // enable the scrolling

Upvotes: 2

santoXme
santoXme

Reputation: 802

Implement RecyclerView.OnItemTouchListener in your call it stole all the touch event on recyclerview

public class RecyclerViewDisabler implements RecyclerView.OnItemTouchListener {

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    return true;
}

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

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean) {

}
}

For enabling and disable the scroll :

RecyclerView recycleview = ...
RecyclerView.OnItemTouchListener disabler = new RecyclerViewDisabler();

recycleview.addOnItemTouchListener(disabler);    // scolling disable
// do what you want to do  at time of disable scrolling
recycleview.removeOnItemTouchListener(disabler); // scrolling enabled again 

Upvotes: 2

Related Questions