All recyclerviews crashes sometimes, when I scroll the list fast, since I've updated to support lib 25.0.0. There is no layout animator and the everything worked fine, with support lib < 25.
The exception is thrown in the RecyclerView, because holder.itemView.getparent() is not null
if (holder.isScrap() || holder.itemView.getParent() != null) {
throw new IllegalArgumentException(
"Scrapped or attached views may not be recycled. isScrap:"
+ holder.isScrap() + " isAttached:"
+ (holder.itemView.getParent() != null));
}
Does anyone else experienced that behavior?
To prevent the crash from this issue, you need to call
setHasStableIds(boolean)
from your adapter and pass the parameter as true:Explanation: The problem occurs when you call
adapter.notifyDataSetChanged();
The
recyclerView
then callsdetachAndScrapAttachedViews(recycler);
It temporarily detaches and scraps all currently attached child views. Views will be scrapped into the givenRecycler
. TheRecycler
may prefer to reuse scrap views.Then
scrapOrRecycleView(recycler, (int) position, (View) child);
is called. This function checks if "hasStableIds" is true or false. If its false then you get the following error :Stable IDs allow the
View
(RecyclerView
,ListView
, etc.) to optimize for the case when items remain the same betweennotifyDataSetChanged
calls.hasStableIds() == true
indicates whether the item ids are stable across changes to the underlying data.If the item ids are stable then it can be reused by the view i.e. "recycled" making the process of re-rendering after the call to
notifyDataSetChanged()
efficient. If item ids are not stable, there is no guarantee that the item has been recycled as there is no way to track them.Note: Setting
setHasStableIds()
to true is not a way to request stable IDs, but to tell Recycler/List/Grid Views that you are providing the said stability.It can also happen if you set
android:orientation="horizontal"
onRecyclerView
in XML. Removing it will prevent the crash.