简体   繁体   English

如何禁用和启用 recyclerview 滚动

[英]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 滚动并在纵向模式下启用它。

 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!感谢您的帮助!

You have to get it done using a custom RecyclerView .您必须使用自定义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 .对于纵向模式,请继续使用正常的RecyclerView

For this issue, I use this one line solution!对于这个问题,我使用了这一行解决方案! :) :)

myRecyclerView.isNestedScrollingEnabled = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM