RecycleView 的Bug

今天在做列表删除的时候,当发现删除最后一行结束后意外的崩溃了,打印了日志发现这更本就不是应用的异常,属于recycleView自己的问题:

W/System.err: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{43856e58 position=0 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
W/System.err: at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505)
W/System.err: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4636)
W/System.err: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
W/System.err: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
W/System.err: at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
W/System.err: at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
W/System.err: at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
W/System.err: at com.softtek.lai.widgets.LinearLayoutManagerWrapper.onLayoutChildren(LinearLayoutManagerWrapper.java:28)
W/System.err: at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979)
W/System.err: at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2904)
W/System.err: at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
W/System.err: at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
W/System.err: at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1192)
W/System.err: at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1697)
W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1460)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1697)
W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1460)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
W/System.err: at android.view.View.layout(View.java:14873)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4653)
W/System.err: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2071)
W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1825)
W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1058)
W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5974)
W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780) 
W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:593)
W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:562)
W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766)
W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5315)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
W/System.err: at dalvik.system.NativeStart.main(Native Method)

从上面的日志可以看出,在底层就崩溃了,谷歌了之后发现只能把这个异常给捕获掉,还是留给谷歌开发人员去解决吧。
处理方法很简单,创建一个类LinearLayoutManagerWrapper继承LinearLayoutManager,重写onLayoutChildren方法

public class LinearLayoutManagerWrapper extends LinearLayoutManager {
    public LinearLayoutManagerWrapper(Context context) {
        super(context);
    }
    public LinearLayoutManagerWrapper(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }
    public LinearLayoutManagerWrapper(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

特此记录一下

你可能感兴趣的:(RecycleView 的Bug)