"Dropped or attached views cannot be recycled," since lib 25.0.0 support

All recyclerviews sometimes crash when I quickly scroll through the list as I update to support lib 25.0.0. There is no layout animator, and everything works fine, with support for lib <25.

An exception is thrown in 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)); } 

Has anyone else experienced this behavior?

+5
source share
2 answers

To avoid a crash in this problem, you need to call setHasStableIds(boolean) from your adapter and pass the parameter as true:

 adapter.setHasStableIds(true); 

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

Then recyclerView calls detachAndScrapAttachedViews(recycler); . It temporarily separates and trims all attached child views. Submissions will be reviewed in this Recycler . Recycler may prefer to reuse scrap types.

Then scrapOrRecycleView(recycler, (int) position, (View) child); is called scrapOrRecycleView(recycler, (int) position, (View) child); . This function checks if the hasStableIds value is true or false. If its false, you will get the following error:

"Discarded or attached species cannot be recycled."

Stable identifiers allow optimizing the View ( recyclerView , ListView , etc.) for the case when the elements remain unchanged between calls to notifyDataSetChanged . hasStableIds() == true indicates whether the item identifiers are resistant to changes in the underlying data.

If the identifiers of the elements are stable, then they can be reused by the view, that is, β€œredesigned”, which makes the process of re-rendering after a successful call to notifyDataSetChanged() . If the identifiers of the elements are unstable, there is no guarantee that the element has been recycled, since there is no way to track them.

Note. Setting setHasStableIds() to true is not a way to request stable identifiers, but tell Recycler / List / Grid Views that you are providing the specified stability.

+8
source

This can also happen if you set android:orientation="horizontal" to RecyclerView in XML. Deleting this file will prevent a crash.

0
source

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


All Articles