禁用RecyclerView滚动并启用ViewPager滚动

时间:2018-08-13 17:25:55

标签: android android-recyclerview android-viewpager

我在viewPager内有一个recyclerView,我已禁用Recyclerview的滚动,但是问题是当我尝试在recyclerview上滑动时,viewpager不会滚动,什么也没发生。我尝试了几种解决方案,但无济于事。 下面是我的代码段。

public class LockableViewPagerMainActivity extends ViewPager {
    private boolean swipeable;

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

    public LockableViewPagerMainActivity(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.swipeable = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.swipeable) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.swipeable) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }

    public void setSwipeable(boolean swipeable) {
        this.swipeable = swipeable;
    }
}

RecyclerView:

recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                // true: consume touch event
                // false: dispatch touch event
                return true;
            }
        });

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

recyclerView.setNestedScrollingEnabled(false);

这将禁用滚动RecyclerView