当RecyclerView遇到Inconsistency detected崩溃时

近来项目里,RecyclerView更新数据偶尔出现crash,错误日志如下:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2064e5c6 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4636)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2904)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1948)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1724)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.Line  

遇到这类错误java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder,通过查阅Stackoverflow和资料,总结了如下情况和解决方法:

方法一

自己写一个继承LinearLayoutManager的包装类,在onLayoutChildren()方法里try-catch捕获该异常。

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    try {
        super.onLayoutChildren(recycler, state);
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}

方法二

在进行数据移除和数据增加时,务必要保证RecyclerView的Adapter中的数据集和移除/添加等操作后的数据集保持一致!

这里,前者是在该Adapter内部,不妨叫做内部数据集,后者是开发人员传过给Adapter的,不妨叫外部数据集。更新RecyclerView数据时,需要保证外部数据集和内部数据集实时保持一致。

外部数据集同步到内部数据集,使用如下的方法:

  • notifyItemRangeRemoved();
  • notifyItemRangeInserted();
  • notifyItemRangeChanged();
  • notifyDataSetChanged();

�这里对notifyDataSetChange()做些说明:
使用该方法的更新内部数据集,没有默认的动画效果,同时更新数据的效率页不如上面的方法,官方不推荐使用这种方式更新数据集。

Don’t call notifyDataSetChanged if you don’t have to.

RecyclerView的Adapter里,发生异常的错误代码如下:

public void notifyData(List<PoiItem> poiItemList) {
    if (poiItemList != null ) {
        mPoiItems.clear();
        mPoiItems.addAll(poiItemList);
        notifyItemRangeChanged(0, poiItemList.size());
    }
}

错误分析:
mPoiItems是外部数据集,对该外部数据集做了两个操作:先移除数据,然后添加数据,之后notify数据集。这里,添加数数据时(Adapter的内部数据集内容还处在外部数据集移除数据之前),造成了内部和外部数据集不一致。

这是一方面,另一方面,我使用了notifyItemRangeChanged()来更新数据,如果poiItemList传过来的新数据和原来mPoiItems的数据数量不一致,就会出现内部数据集和外部数据在同步后不一致,从而报错。

修复后,运行正常的代码如下:

public void notifyData(List<PoiItem> poiItemList) {
    if (poiItemList != null) {
        int previousSize = mPoiItems.size();
        mPoiItems.clear();
        notifyItemRangeRemoved(0, previousSize);
        mPoiItems.addAll(poiItemList);
        notifyItemRangeInserted(0, poiItemList.size());
    }
}

修复分析:

每一次对外部数据集做改动时,都需要紧接着主动对外部数据集和内部数据集做一次同步操作,动画效果也得到了保持(使用方法三,动画效果缺失)

方法三:

使用notifyDataSetChanged同步外部数据集和内部数据集。该方法简单,但是失去了动画效果,并且更新数据的性能低。

use notifyDataSetChanged() will avoid this crash, but it will kill Animation and Performance.

另外,如果对外部数据集做了二次以上的操作,却只调用notifyDataSetChanged同步一次,也很有可能会报上文所示的错误。

参考文章:

1.stackoverflow 1

2.RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案

3.使用RecyclerView遇到的一些问题 Inconsistency detected

    原文作者:sugaryaruan
    原文地址: https://www.jianshu.com/p/2eca433869e9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞