7

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?

JPLauber
  • 1,361
  • 1
  • 14
  • 22
  • You may have better luck if you provide a [mcve] demonstrating the problem, along with the full stack trace of the crash. – CommonsWare Oct 26 '16 at 13:26
  • same issue here and it also only happened after I upgraded support library to version 25.0.0. Some body create an issue here: https://code.google.com/p/android/issues/detail?id=226353&can=1&q=reportedby%3DDeveloper%20-has%3Atriaged%20-has%3Apriority%20component%3DSupport-Libraries%20-has%3Ablocked&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&start=1400 but it looks like the google support library team close it since lack of reproduce step. – Anthonyeef Nov 25 '16 at 04:11
  • 1
    BTW I only face this issue under GridLayoutManager. Where I use LinearLayoutManager, it looks all fine. – Anthonyeef Nov 25 '16 at 04:16

2 Answers2

11

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.

  • would u please assist me with quesiton http://stackoverflow.com/questions/41408085/gapworker-with-scrapped-or-attached-views-may-not-be-recycled-isscrapfalse-isa – Reprator Jan 03 '17 at 11:16
-1

It can also happen if you set android:orientation="horizontal" on RecyclerView in XML. Removing it will prevent the crash.

Ali Kazi
  • 1,561
  • 1
  • 15
  • 22
  • Anyone who wants to downvote this, please add that line to your `RecyclerView` and check if you get this crash. – Ali Kazi Nov 13 '17 at 03:53