SwipeRefreshLayout快速刷新引起的bug

SwipeRefreshLayout+RecyclerView
如果快速不停地刷新页面,会报异常

 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:10

at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4957)
                                                                              at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
                                                                              at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                                                                              at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:531)
                                                                              at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                                                                              at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
                                                                              at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
                                                                              at android.support.v7.widget.GridLayoutManager.scrollVerticallyBy(GridLayoutManager.java:372)
                                                                              at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4357)
                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:817)
                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:620)
                                                                              at android.view.Choreographer.doFrame(Choreographer.java:588)
                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:803)
                                                                              at android.os.Handler.handleCallback(Handler.java:815)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                              at android.os.Looper.loop(Looper.java:194)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5572)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)

这是由于 adapter.notifyDataSetChanged()频繁调用引起的,参考
RecyclerView的BUG探讨
RecyclerView: Inconsistency detected. Invalid item position

第二篇文章中投票最高的一个解决方案是,自定义一个GridLayoutManager(或者LinearLayoutManager),重写supportsPredictiveItemAnimations,让其返回false

实践证明:该方案在一定程度上可行,但有时还会偶尔出现bug

你可能感兴趣的:(Bug库,基本控件用法)