GapWorker with Scrapped or attached views may not be recycled. isScrap:false isAttached:true

4.2k views Asked by At

When i scroll my recyclerview too fast i am getting the following crash

 java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true
                                                                 at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5659)
                                                                 at android.support.v7.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:5603)
                                                                 at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:277)
                                                                 at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:324)
                                                                 at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:337)
                                                                 at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:344)
                                                                 at android.support.v7.widget.GapWorker.run(GapWorker.java:370)
                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I had tried my every effort and get similar question on stackoverflow, but none of them were helpful like following links,

Link 1

Link 2

Link 3

Link 4

Link 5

I have no knowledge of this crash from where it is occurring. Any help would be greatly appreciated. I am neither using "android:animateLayoutChanges="true"

Basically i had tried to merge these two different GitHub project,

rv-adapter-states

rv-adapter-endless

My files links are as follows from pastbin,

Row Layout

Code of recyclerview

My Adapter merge from two diffrent adapter of rv

Main adapter

3

There are 3 answers

0
asamoylenko On

Have you tried layoutManager.setItemPrefetchEnabled(false);? It worked for me.

0
aLx On

I had the same problem and I don't know why is happening.

I solved it removing the android:animateLayoutChanges on the recyclerview and to animate the recyclerview I have added: recyclerView.setItemAnimator(new DefaultItemAnimator());

Take an account to add the method public long getItemId(int position) and set setHasStableIds(true) if you want animate it.

0
Mu Sa On

In my case the problem was because of the incorrect implementing of this method public long getItemId(int position) (overrided from RecyclerView.Adapter method).

The old code will get two different ids for the same item (in my case it is the footer item), after fixing the implementation the issue has gone.