Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Scrapped or attached views may not be recycled" since support lib 25.0.0

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?

like image 941
JPLauber Avatar asked Oct 26 '16 13:10

JPLauber


1 Answers

To prevent the crash from this issue, you need to call setHasStableIds(boolean) from your adapter and pass the parameter as true:

adapter.setHasStableIds(true);

Explanation: The problem occurs when you call adapter.notifyDataSetChanged();

The recyclerView then calls detachAndScrapAttachedViews(recycler); It temporarily detaches and scraps all currently attached child views. Views will be scrapped into the given Recycler. The Recycler 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 :

"Scrapped or attached views may not be recycled."

Stable IDs allow the View (RecyclerView, ListView, etc.) to optimize for the case when items remain the same between notifyDataSetChanged 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.

like image 169
Sabin Bajracharya Avatar answered Feb 20 '23 10:02

Sabin Bajracharya