RecyclerView问题:java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adap

###写在前面
在使用RecyclerView中出现了一个问题,如下图
这里写图片描述
###解决办法:
我们在使用RecycxlerView时,通常先回设置一个LayoutManager
我们的问题出现在LayoutManageronLayoutChildren方法中,我们只要在onLayoutChildren中捕获这个异常即可,下面我以LinearLayoutManager为例。

先写一个类LinearLayoutManagerWrap继承LinearLayoutManager,然后重新onLayoutChildren方法即可,如下代码:

public class LinearLayoutManagerWrap extends LinearLayoutManager {
    public LinearLayoutManagerWrap(Context context) {
        super(context);
    }

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

    public LinearLayoutManagerWrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
	    // 在这里通过try来捕获这个异常即可
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

然后把LinearLayoutManagerWrap设置RecyclerView即可

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值