今天遇到的一個Bug,之前的項目中也遇到過,這里紀錄一下,如果下次再遇到,可以直接拿來用。這個算是Android官方的一個Bug,遲遲沒有解決,網(wǎng)上可以看到一大群人遇到該問題。
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21(offset:21.state:20
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3300)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1093)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:956)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:2715)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
復現(xiàn)步驟是,對有數(shù)據(jù)的列表刷新操作,在還未刷出數(shù)據(jù)的時候,不斷的滑動,Crash。
很容易理解,也就是在Recyclerview滑動的時候,執(zhí)行notifyDataSetChanged()導致的。
網(wǎng)上有很多人遇到了這個問題,回答也是千奇百怪。
自己繼承LinearLayoutManager
復寫下面的方法,返回false,思路是不執(zhí)行部分動畫。
@Overridepublic boolean supportsPredictiveItemAnimations() {
return false;
}
然而然并卵,依然會crash。
還有很多人回答在數(shù)據(jù)add,clear的時候馬上執(zhí)行notifyDataSetChanged
然并卵,不管你什么時候執(zhí)行,都會crash。
刷新的時候不讓用戶滑動
這種方式肯定是可以的,屏蔽掉了滑動事件,肯定不會有這個沖突。
mRecyclerView.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mIsRefreshing) {
return true;
} else {
return false;
}
}
}
);
//當刷新時設置
//mIsRefreshing=true;
//刷新完畢后還原為false
//mIsRefreshing=false;
但是代價太大,太影響用戶體驗。產(chǎn)品肯定不會同意,Pass。
最后當然是大招了
在執(zhí)行notifyDataSetChanged之前,現(xiàn)判斷當前時候還在滑動,如果沒有滑動,執(zhí)行notifyDataSetChanged方法。
if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE ||
!mRecyclerView.isComputingLayout()) {
mAdapter.notifyDataSetChanged();
}
這種方式在用戶沒有滑動操作的時候刷新,有滑動,也不用怕,因為數(shù)據(jù)已經(jīng)替換為線上返回的數(shù)據(jù),在用戶滑動的時候依然會刷新。
親測,可行。