How to Disable Recyclerview Scrolling

How to disable RecyclerView scrolling?

You should override the layoutManager of your recycleView for this. This way it will only disable scrolling, none of the other functionalities. You will still be able to handle click or any other touch events. For example:-

Original:

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

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

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

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

Here using "isScrollEnabled" flag you can enable/disable scrolling functionality of your recycle-view temporarily.

Also:

Simple override your existing implementation to disable scrolling and allow clicking.

linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};

In Kotlin:

object : LinearLayoutManager(this){ override fun canScrollVertically(): Boolean { return false } }

How to disable and enable the recyclerview scrolling

You have to get it done using a custom RecyclerView. Initialize it programmatically when the user is in landscape mode and add this view to your layout:

public class MyRecycler extends RecyclerView {

private boolean verticleScrollingEnabled = true;

public void enableVersticleScroll (boolean enabled) {
verticleScrollingEnabled = enabled;
}

public boolean isVerticleScrollingEnabled() {
return verticleScrollingEnabled;
}

@Override
public int computeVerticalScrollRange() {

if (isVerticleScrollingEnabled())
return super.computeVerticalScrollRange();
return 0;
}


@Override
public boolean onInterceptTouchEvent(MotionEvent e) {

if(isVerticleScrollingEnabled())
return super.onInterceptTouchEvent(e);
return false;

}

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

public MyRecycler(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public MyRecycler(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}

For portrait mode keep using your normal RecyclerView.

how to disable recycler view scrolling so that it listens to Scrollview layout?

Logically, it is not a good idea to put ListView inside a ScrollView. However, if you insist then:

  • You may either increase the ListView height based on the sum of its
    rows height as mentioned here.
  • Or let the recycling in place but intercept the touch on ListView
    to redirect scrolling to its parent ScrollView as mentioned
    here.


Related Topics



Leave a reply



Submit