1. 程式人生 > >RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

not ext title size data app position line override

今天使用RecyclerView時,上下兩個RecyclerView,在實現下拉刷新時,報錯:

java.lang.IndexOutOfBoundsException: Inconsistency detected.
Invalid view holder adapter positionViewHolder{56798b2 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}

在網上看到這個方法可以暫時性解決問題

其實也不是什麽解決方法,只是把這個異常捕獲了,不讓他奔潰了,這個問題的終極解決方案還是得讓google去修復。

1、創建一個類LinearLayoutManagerWrapper繼承LinearLayoutManager,重寫onLayoutChildren方法

[java] view plain copy
  1. public class WrapContentLinearLayoutManager extends LinearLayoutManager {
  2. public WrapContentLinearLayoutManager(Context context) {
  3. super(context);
  4. }
  5. public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
  6. super(context, orientation, reverseLayout);
  7. }
  8. public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  9. super(context, attrs, defStyleAttr, defStyleRes);
  10. }
  11. @Override
  12. public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
  13. try {
  14. super.onLayoutChildren(recycler, state);
  15. } catch (IndexOutOfBoundsException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

2、設置RecyclerView的布局管理為WrapContentLinearLayoutManager對象

[java] view plain copy
  1. mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.