RecyclerView-IndxeOutOfBoundsException

RecycleView的Bug : java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position

最近在工作上,因為某些原因,需要很快速地刷新RecycleView內的元素,但是很常出現閃退,追了一下log,發現是下列的原因:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 150(offset:150).state:153
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1618)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4702)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Bug 出現原因 :

再進行數據變動時,務必要保證 Adapter 中的數據和移除的數據保持一致,就是當你更新你的集合後,呼叫 notify 方法時, adapter 的更新預期結果和實際集合更新結果不同,就會出現此異常。

例如: 增加了兩條數據,但是 Adapter 的 notify 只增加 5 條數據,這種情況就屬於不一致,簡單來說 Apapter 有個 Size,你的資料結構集合也有 Size,這兩個 Size 在呼叫 notify 方法時必須保持相同。

解決方法 :

覆寫 xxxxLayoutManager(看你使用的是 LinearLayoutManager 或是 GridLayoutManager或其他)

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
public class RecyclerViewLinearLayoutManager extends LinearLayoutManager {
public RecyclerViewLinearLayoutManager(Context context) {
super( context );
}

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

public RecyclerViewLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super( context, attrs, defStyleAttr, defStyleRes );
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
//主要在這邊加入 try catch
super.onLayoutChildren( recycler, state );
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}

}
}

主要是在添加集合的時候,增加了一層 try catch去解決。

參考 :

https://stackoverflow.com/questions/30458640/recyclerview-java-lang-indexoutofboundsexception-inconsistency-detected-inval

http://www.cnblogs.com/fuyaozhishang/p/6991221.html