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


今天使用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.   
  6.     public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {  
  7.         super(context, orientation, reverseLayout);  
  8.     }  
  9.   
  10.     public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {  
  11.         super(context, attrs, defStyleAttr, defStyleRes);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {  
  16.         try {  
  17.             super.onLayoutChildren(recycler, state);  
  18.         } catch (IndexOutOfBoundsException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  

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

 

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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM