当RecycleView跟ScrollView冲突设置自定义LinearLayoutManager的时候出现IllegalArgumentException异常

很多情况的时候我们会用到RecycleView去代替原始的listView,这样我们就可能会碰到一样的问题,RecycleView与ScrollView滑动冲突,那么我们最佳的解决方案是禁止RecycleView的滑动,让外面的ScrollView整体滑动。那么我们需要重写RecycleView的linearLayoutManager

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;

/**
 * Created by 唐小糖 on 2017/8/11.
 * 重写RecyclerView 的LinearLayoutManager
 * 解决ScrollView中嵌套重写RecyclerView会导致滑动RecyclerView没有惯性效果
 */

public 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();
    }
}
然后 我们在activity中给RecycleView设置属性即可

            MyLayoutManager myLayoutManager = new MyLayoutManager(this);
            myLayoutManager.setScrollEnabled(false);

            assetsAdapter = new AssetsAdapter(this, list);
            mRecyclerView.setLayoutManager(myLayoutManager);
            mRecyclerView.setAdapter(assetsAdapter);

但是可能某些小伙伴会出现IllegalArgumentException异常,那么是因为你只创建了一个MyLayoutManager却绑定了好几个RecyclerView的原因, MyLayoutManager一个对象只能绑定一个RecyclerView,因此你有多个RecyclerView的时候请实例化对应数量的MyLayoutManager,去绑定RecyclerView

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青丶穗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值