最近使用RecyclerView的一个页面一直在疯狂报错,但是不是必现,摸奖式复现。
报错内容如下:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:11 android.support.v7.widget.RecyclerView{8ace96a VFED..... ......ID 0,0-1080,1523 #7f090107 app:id/home_recycler_view}, adapter:com.beiins.baseRecycler.base.RViewAdapter@2b9de5b, layout:android.support.v7.widget.LinearLayoutManager@4596f8, context:com.beiins.dolly.activity.HomeActivity@29cea07
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5817)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at com.scwang.smartrefresh.layout.SmartRefreshLayout.onLayout(SmartRefreshLayout.java:684)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:761)
at android.view.View.layout(View.java:19612)
at android.view.ViewGroup.layout(ViewGroup.java:6055)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2246)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1426)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6873)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
at android.view.Choreographer.doCallbacks(Choreographer.java:841)
at android.view.Choreographer.doFrame(Choreographer.java:772)
有用的信息就是java.lang.IndexOutOfBoundsException: Inconsistency detected.这啥意思?数组越界!确实是对数据源有删除操作,但是为什么不是必现呢?
因为我是在子线程中删除的数据源,导致RecyclerView在onLayoutChildren的时候不知道有一个数据已经被移除了,所以出现数组越界。
网上有几个的解决方案都是什么重写LinearLayoutManager,这根本治标不治本。记录一下,方便遇到这个问题的人。