Shikha Ratra
Shikha Ratra

Reputation: 695

How to disable and enable the recyclerview scrolling

I want to disable recyclerview scrolling in landscape mode and enable it in the portrait mode.

 recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            // Stop only scrolling.
            return rv.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING;
        }
    });

I am using this method to disable scrolling but can't find a way to enable it again.

Thanks for any help!

Upvotes: 1

Views: 13735

Answers (3)

kalgik
kalgik

Reputation: 1

In my case, I remove and add OnScrollListener manually:

recyclerView.removeOnScrollListener(this);

Upvotes: 0

Kaaveh Mohamedi
Kaaveh Mohamedi

Reputation: 1795

For this issue, I use this one line solution! :)

myRecyclerView.isNestedScrollingEnabled = false

Upvotes: 1

Devendra Singh
Devendra Singh

Reputation: 2514

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.

Upvotes: 5

Related Questions