Android RecyclerView异步更新数据导致的崩溃问题

          Android RecyclerView异步更新数据导致的崩溃问题

 

之前写极光即时通讯UI的时候,发现的问题,今天突发奇想,来分享给大家.


问题症状:


如果绑定的集合List中的数据和RecycerView的数据不一致的时候 RecycerView就会崩溃

 

报错信息:

1 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588
 2         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3300)
 3         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258)
 4         at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803)
 5         at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302)
 6         at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
 7         at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1093)
 8         at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:956)
 9         at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:2715)
10         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11         at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12         at android.view.Choreographer.doFrame(Choreographer.java:524)
13         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
14         at android.os.Handler.handleCallback(Handler.java:615)
15         at android.os.Handler.dispatchMessage(Handler.java:92)
16         at android.os.Looper.loop(Looper.java:137)
17         at android.app.ActivityThread.main(ActivityThread.java:4921)
18         at java.lang.reflect.Method.invokeNative(Native Method)
19         at java.lang.reflect.Method.invoke(Method.java:511)
20         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
21         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
22         at dalvik.system.NativeStart.main(Native Method)

解决方案:

mRecycler.setLayoutManager(new WrapContentLinearLayoutManager(this));

public class WrapContentLinearLayoutManager  extends LinearLayoutManager {


    public WrapContentLinearLayoutManager(Context context) {
        super(context);
    }

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

    public WrapContentLinearLayoutManager(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();
        }
    }

}

文章借鉴于:https://www.cnblogs.com/liushilin/p/6927612.html

你可能感兴趣的:(Android)