转载自http://blog.csdn.net/w_lin/article/details/53019888
最近发现recyclerView快速滑动时会崩溃,百度发现是Inconsistency detected. Invalid item position 16(offset:16).state:25异常,因此我对事件进行拦截,发现依旧不查了半天才找到这个博主写的解决了我的问题,分享给大家,首先我们需要重写LinearLayoutManager方法
昨天遇到一个神奇并且很难缠的bug……
RecyclerView上下滑动的时候崩溃,崩溃信息如下:
java.lang.IndexOutOfBoundsException
Inconsistency detected. Invalid item position 16(offset:16).state:64
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4957)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4357)
android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
android.view.Choreographer.doCallbacks(Choreographer.java:606)
android.view.Choreographer.doFrame(Choreographer.java:574)
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5601)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
搜遍了Google,百度,github,stackoverflow……都没有解决,算了自己动手丰衣足食。经过两天研究这么解决了:
/**
* 去除动画防止,防止出现recyclerView的bug
* Created by zhanglin on 2016/11/3.
*/
public class MyLinearLayoutManager extends LinearLayoutManager {
public MyLinearLayoutManager(Context context) {
super(context);
}
@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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
Java中如下:
MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager (mContext);
recyclerView.setLayoutManager(myLinearLayoutManager );
- 1
- 2
解决了,好开心哈哈哈
<link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-d4dade9c33.css">
</div>