近來項目里跟压,RecyclerView更新數(shù)據(jù)偶爾出現(xiàn)crash胰蝠,錯誤日志如下:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2064e5c6 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4636)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2904)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1948)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1724)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)
at android.widget.FrameLayout.onLayout(FrameLayout.java:568)
at android.view.View.layout(View.java:15912)
at android.view.ViewGroup.layout(ViewGroup.java:5108)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.Line
遇到這類錯誤java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder,通過查閱Stackoverflow和資料震蒋,總結(jié)了如下情況和解決方法:
方法一
自己寫一個繼承LinearLayoutManager的包裝類茸塞,在onLayoutChildren()方法里try-catch捕獲該異常。
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
方法二
在進(jìn)行數(shù)據(jù)移除和數(shù)據(jù)增加時查剖,務(wù)必要保證RecyclerView的Adapter中的數(shù)據(jù)集和移除/添加等操作后的數(shù)據(jù)集保持一致钾虐!
這里,前者是在該Adapter內(nèi)部笋庄,不妨叫做內(nèi)部數(shù)據(jù)集效扫,后者是開發(fā)人員傳過給Adapter的,不妨叫外部數(shù)據(jù)集直砂。更新RecyclerView數(shù)據(jù)時菌仁,需要保證外部數(shù)據(jù)集和內(nèi)部數(shù)據(jù)集實時保持一致。
外部數(shù)據(jù)集同步到內(nèi)部數(shù)據(jù)集静暂,使用如下的方法:
- notifyItemRangeRemoved();
- notifyItemRangeInserted();
- notifyItemRangeChanged();
- notifyDataSetChanged();
?這里對notifyDataSetChange()做些說明:
使用該方法的更新內(nèi)部數(shù)據(jù)集济丘,沒有默認(rèn)的動畫效果,同時更新數(shù)據(jù)的效率頁不如上面的方法洽蛀,官方不推薦使用這種方式更新數(shù)據(jù)集摹迷。
Don’t call notifyDataSetChanged if you don’t have to.
RecyclerView的Adapter里,發(fā)生異常的錯誤代碼如下:
public void notifyData(List<PoiItem> poiItemList) {
if (poiItemList != null ) {
mPoiItems.clear();
mPoiItems.addAll(poiItemList);
notifyItemRangeChanged(0, poiItemList.size());
}
}
錯誤分析:
mPoiItems是外部數(shù)據(jù)集郊供,對該外部數(shù)據(jù)集做了兩個操作:先移除數(shù)據(jù)泪掀,然后添加數(shù)據(jù),之后notify數(shù)據(jù)集颂碘。這里,添加數(shù)數(shù)據(jù)時(Adapter的內(nèi)部數(shù)據(jù)集內(nèi)容還處在外部數(shù)據(jù)集移除數(shù)據(jù)之前)椅挣,造成了內(nèi)部和外部數(shù)據(jù)集不一致头岔。
這是一方面,另一方面鼠证,我使用了notifyItemRangeChanged()來更新數(shù)據(jù)峡竣,如果poiItemList傳過來的新數(shù)據(jù)和原來mPoiItems的數(shù)據(jù)數(shù)量不一致,就會出現(xiàn)內(nèi)部數(shù)據(jù)集和外部數(shù)據(jù)在同步后不一致量九,從而報錯适掰。
修復(fù)后颂碧,運行正常的代碼如下:
public void notifyData(List<PoiItem> poiItemList) {
if (poiItemList != null) {
int previousSize = mPoiItems.size();
mPoiItems.clear();
notifyItemRangeRemoved(0, previousSize);
mPoiItems.addAll(poiItemList);
notifyItemRangeInserted(0, poiItemList.size());
}
}
修復(fù)分析:
每一次對外部數(shù)據(jù)集做改動時,都需要緊接著主動對外部數(shù)據(jù)集和內(nèi)部數(shù)據(jù)集做一次同步操作类浪,動畫效果也得到了保持(使用方法三载城,動畫效果缺失)
方法三:
使用notifyDataSetChanged同步外部數(shù)據(jù)集和內(nèi)部數(shù)據(jù)集。該方法簡單费就,但是失去了動畫效果诉瓦,并且更新數(shù)據(jù)的性能低。
use notifyDataSetChanged() will avoid this crash, but it will kill Animation and Performance.
另外力细,如果對外部數(shù)據(jù)集做了二次以上的操作睬澡,卻只調(diào)用notifyDataSetChanged同步一次,也很有可能會報上文所示的錯誤眠蚂。
參考文章: