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.
source share