RecycleView冲突这点事儿!

ScrollView嵌套RecyclerView滑动冲突,禁止RecycleView滑动

ScrollView中嵌套了几个RecyclerView,会导致滑动RecyclerView没有惯性效果。
解决这个问题可以通过重写RecyclerView 的 LinearLayoutManager。
class MyLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled = true;

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

    public MyLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public MyLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    /**
     * 是否支持滑动
     * @param flag
     */
    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {
        //isScrollEnabled:recyclerview是否支持滑动
        //super.canScrollVertically():是否为竖直方向滚动
        return isScrollEnabled && super.canScrollVertically();
    }
}

这里主要是重写canScrollVertically()这个方法,return true支持滑动竖直方向滑动,return false反之。
myLayoutManager.setScrollEnabled(false);
  • 1
  • 1
使用时设置true,false就可以了!

做了个项目,用了support包里的SwipeRefreshLayout和RecyclerView。两者一起使用有一点点小问题,有时候拉着拉着,列表还没拉玩就出来刷新的图标了,在华为荣耀上尤为明显。
解决这个问题如下:

mLinearLayoutManager = new LinearLayoutManager(mActivity);

        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override

            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                super.onScrolled(recyclerView, dx, dy);

             

                swipeRefreshLayout.setEnabled(mLinearLayoutManager.findFirstVisibleItemPosition() == 0);

            }

        });

这样就可以确保只有完全滑到顶部,然后再下拉时才刷新!

猜你喜欢

转载自blog.csdn.net/Angle0306/article/details/72779621
今日推荐