08-07 09:51:13.414 26357-26357/..............android E/AndroidRuntime: FATAL EXCEPTION: main Process: com.wkhgs.b2b2c2016.android, PID: 26357 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 8(offset:8).state:20 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4368) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327) at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1155) at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1012) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3777) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933) at android.view.Choreographer.doCallbacks(Choreographer.java:742) at android.view.Choreographer.doFrame(Choreographer.java:671) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6577) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 前段时间遇到的一个BUG算是老BUG
重写下LinearLayoutManager 就可以解决这个问题。
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 boolean supportsPredictiveItemAnimations() { return false; } public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { try { super.onLayoutChildren(recycler, state); } catch (IndexOutOfBoundsException e) { e.printStackTrace(); } } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { try { return super.scrollVerticallyBy(dy, recycler, state); } catch (Exception e) { e.printStackTrace(); } return 0; } }
