RecyclerView bug -- Inconsistency detected, recyclerview
How to reproduce bugs
When RecyclerView and SwipeRefreshLayout are used for pull-down refreshing, if the bound List object is clear before data is updated, then the user will quickly slide the RecyclerView, causing a crash, moreover, exceptions are not reported to our Code and are internal RecyclerView errors.
Possible causes
After the list is cleared, the system quickly slides up and the new data has not arrived. As a result, when RecyclerView wants to update and load the following Item, the data source cannot be found, causing crash. The following error will be reported:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588 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)
Solution
When refreshing, that is, clear, the RecyclerView cannot be slide for the time being, and then slide. The Code adds whether to refresh and then intercept the gesture during RecyclerView initialization. A boolean variable is required to determine whether a refresh is being performed. Set this variable to true upon refresh and false upon refresh, the related code is as follows:
Private boolean mIsRefreshing = false; mRecyclerView. setOnTouchListener (new View. onTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {if (mIsRefreshing) {return true ;}else {return false ;}}}); // when refreshing, set // mIsRefreshing = true; // restore to false after refreshing. // mIsRefreshing = false;
Summary
It is obvious that hexadecimal sliding is unwise, but this is also a solution before any other solution is found.