GapWorker with broken or attached views cannot be recycled. isScrap: false isAttached: true

When I scan my recyclerview too fast, I get 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 tried my best and asked a similar question in stackoverflow, but none of them were useful as the following links,

Link 1

Link 2

Link 3

Link 4

Link 5

I do not know about this failure, where it comes from. Any help would be greatly appreciated. I do not use "android: animateLayoutChanges =" true "

I basically tried to combine these two different GitHub projects,

rv-adapter-states

rv-adapter-endless

My files are related to pastbin as follows:

Line layout

Recyclerview code

My adapter integrates with two different rv adapters

Main adapter

+5
source share
3 answers

In my case, the problem was due to improper execution of this method public long getItemId(int position) (overridden from the RecyclerView.Adapter method).

The old code will receive two different identifiers for the same element (in my case, this is a footer element), after fixing the implementation, the problem disappeared.

0
source

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

0
source

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

I decided to remove t2 on recyclerview and revive the recyclerview, which I added: recyclerView.setItemAnimator(new DefaultItemAnimator());

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

0
source

Source: https://habr.com/ru/post/1258830/


All Articles