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

来源:互联网 发布:淘宝联盟怎么查订单号 编辑:程序博客网 时间:2024/06/06 09:26
参考链接:

https://my.oschina.net/u/1446823/blog/712306

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);
使用的时候只要设置这行代码就行。
0 2
原创粉丝点击