如何禁用和启用 recyclerview 滚动

Shi*_*tra 1 android android-recyclerview

我想在横向模式下禁用 recyclerview 滚动并在纵向模式下启用它。

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

我正在使用此方法禁用滚动,但找不到再次启用它的方法。

谢谢你的帮助!

Dev*_*ngh 5

您必须使用自定义RecyclerView. 当用户处于横向模式时以编程方式初始化它并将此视图添加到您的布局中:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于纵向模式,请继续使用您的正常RecyclerView.