2

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

user3606902
  • 829
  • 1
  • 10
  • 25

4 Answers4

2

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 
santoXme
  • 802
  • 5
  • 20
2
// 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
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
2

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

droidev
  • 7,352
  • 11
  • 62
  • 94
1

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 }
}
Md. Arif
  • 448
  • 1
  • 5
  • 13