“废弃或附加的视图可能无法回收"自支持lib 25.0.0起 [英] "Scrapped or attached views may not be recycled" since support lib 25.0.0

查看:39
本文介绍了“废弃或附加的视图可能无法回收"自支持lib 25.0.0起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我快速滚动列表时,所有recyclerviews有时会崩溃,因为我已更新为支持lib 25.0.0.没有布局动画设计器,一切都运行良好,并支持lib< 25.

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.

由于holder.itemView.getparent()不为空,在RecyclerView中引发了异常

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?

推荐答案

为防止此问题导致崩溃,您需要从适配器调用setHasStableIds(boolean)并将参数传递为true:

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);

说明: 当您调用adapter.notifyDataSetChanged();

recyclerView然后调用detachAndScrapAttachedViews(recycler);.它临时分离并剪贴所有当前附加的子视图.视图将被废弃到给定的Recycler中. Recycler可能更愿意重复使用剪贴簿视图.

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.

然后调用scrapOrRecycleView(recycler, (int) position, (View) child);.此函数检查"hasStableIds"是true还是false.如果它为假,那么您将收到以下错误:

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."

稳定ID允许View(RecyclerViewListView等)针对在notifyDataSetChanged调用之间项目保持相同的情况进行优化. hasStableIds() == true表示项目ID在基础数据的更改中是否稳定.

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.

如果商品ID稳定,则视图可以重复使用它,即回收",从而使对notifyDataSetChanged()的调用后的重新渲染过程高效.如果商品ID不稳定,则由于无法跟踪它们,因此无法保证该商品已被回收.

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.

注意:将setHasStableIds()设置为true并不是请求稳定ID的一种方法,而是告诉Recycler/List/Grid视图您正在提供所述稳定性.

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.

这篇关于“废弃或附加的视图可能无法回收"自支持lib 25.0.0起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆