RecyclerView crashes when “scrapped or attached views may not be recycled”

前端 未结 27 2630
灰色年华
灰色年华 2020-11-29 19:50

I\'m using a simple implementation of RecyclerView taken from the Android website using a StaggeredGridLayoutManager and I keep getting this error

相关标签:
27条回答
  • 2020-11-29 20:15

    There are a number of reason why this exception is called. In my case it was due to animations running thats why the views are still attached and could not be removed to the view. Only when the animation is finished thus the view could be removed and recycled.

    There are two types of animation that could affect the recyclerview recycling.

    1) Is the RecyclerView.ItemAnimator - this should not be the problem. This should be pretty much safe to use as it check for attach and scrapped views and handle recycling properly.

    2) android:animateLayoutChanges="true" or TransitionManager.beginDelayedTransition() or TransitionManager.go(), etc. - These animations runs on its own and take hold of the items to animate. This result to the views being force to be attached until the animation is finished. The recyclerview do not have any knowledge of these animations since it is outside its scope. Therefore the recyclerview might try to recycle an item thinking it could be recycled properly but the problem is that these APIs are still holding on to the views until there animation is finished.

    If you are using android:animateLayoutChanges="true" or TransitionManager.beginDelayedTransition() or TransitionManager.go(), etc. simply remove the RecyclerView and its children from the animation.

    You can simply do this by taking hold of the Transition and calling

    Transition.excludeChildren(yourRecyclerView, true)
    Transition.excludeTarget(yourRecyclerView, true)
    

    Note:

    Take note that it is important to use Transition.excludeChildren() to exclude all of Recyclerview children from the animation and not just the Recyclerview itself.

    0 讨论(0)
  • 2020-11-29 20:16

    In my case, I used the TransitionManager.beginDelayedTransition() before adding a view on top of the recyclerView. I removed the TransitionManager.beginDelayedTransition() and no crash.

    0 讨论(0)
  • 2020-11-29 20:16

    I solved this issue by calling

    setHasStableIds(true);
    

    in the adapter's constructor and overriding getItemId in the adapter:

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    0 讨论(0)
  • 2020-11-29 20:17

    I had this issue because I overwrite equals() and hashcode() method of ViewHolder of RecyclerView.ViewHolder by calculating data equality and hashcode, then the recycle logic did not work and crashed, I just remove the overwrite and fixed.

    0 讨论(0)
  • 2020-11-29 20:20

    For me the same bug with caused by a LayoutTransition on a higher level ViewGroup.

    0 讨论(0)
  • 2020-11-29 20:21

    1、remove:remove data from list.

    2、notifyDataSetChanged:notifyDataSetChanged();

    3、notifyItemRemoved:show animation.

    4、notifyItemRangeChanged:range view size and redraw the viewHolders(onBindViewHolder methods)

    0 讨论(0)
提交回复
热议问题