IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder...

第一种情况
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)
第二种情况
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2181e078 position=-1 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at com.scwang.smartrefresh.layout.impl.RefreshContentWrapper.layout(RefreshContentWrapper.java:262)
at com.scwang.smartrefresh.layout.SmartRefreshLayout.onLayout(SmartRefreshLayout.java:605)
第三种情况
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{b7bdc29 position=35 id=-1, oldPos=-1, pLpos:-1 no parent} android.support.v7.widget.RecyclerView{c637ee4 VFED..... .F...... 0,0-1080,1707 #7f0f028a app:id/qx}, adapter:com.bilin.huijiao.music.server.LiveMusicListAdapter@f5eaf4d, layout:com.bilin.support.CustomLinearLayoutManager@7484a02, context:com.bilin.huijiao.music.server.MusicActivity@4903825
    at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(SourceFile:5421)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(SourceFile:5603)
    at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(SourceFile:285)
    at android.support.v7.widget.GapWorker.flushTaskWithDeadline(SourceFile:342)
    at android.support.v7.widget.GapWorker.flushTasksWithDeadline(SourceFile:358)
    at android.support.v7.widget.GapWorker.prefetch(SourceFile:365)
    at android.support.v7.widget.GapWorker.run(SourceFile:396)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6321)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:829)

网上的解决方案很多是针对第一种的,仔细看第一种的调用路径中有一个LinearLayoutManager.onLayoutChildren,所以有人就建议重写LinearLayoutManager的onLayoutChildren方法,catch住这个异常,具体如下:

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

这个确实能起作用,但是时不时还是会报另外两种情况的异常,而且没办法通过重写来catch住,找了很多资料都没找到解决方案。看了异常的出处在RecyclerView#validateViewHolderForOffsetPosition(ViewHolder holder)中

if (holder.mPosition < 0 || holder.mPosition >= mAdapter.getItemCount()) {
                throw new IndexOutOfBoundsException("Inconsistency detected. Invalid view holder "
                        + "adapter position" + holder);
            }

从日志看holder.mPosition肯定是>0的,也就是说只有>=holder.mPoition的情况,第一反应是是不是存在一种极限情况是在添加ViewHolder的时候刚好有一个Item正好被删除了导致mAdapter.getItemCount()变小了。后面从逻辑上排除基本上不存在这个操作。然后就想是不是RecyclerView本身的Bug,然后就去看了一下support包的更新日志,在27.1.0上发现了一处有用的:


image.png

翻译过来就是:

*   RecyclerView IndexOutOfBoundsException,因为在setAdapter中未清除State.mPreviousLayoutItemCount(AOSP issue [37657125](https://issuetracker.google.com/issues/37657125))

接下大家应该懂怎么做了吧,把自己项目的support包升级到27.1.0以上就可以了。
-com.android.support:appcompat-v7:27.1.0
或者
-com.android.support:recyclerview-v7:27.1.0
看你自己
最后附上support包的更新日志连接

你可能感兴趣的:(IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder...)