gpt4 book ai didi

android - "Scrapped or attached views may not be recycled"因为支持 lib 25.0.0

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:00 25 4
gpt4 key购买 nike

当我快速滚动列表时,所有 recyclerviews 有时会崩溃,因为我已经更新以支持 lib 25.0.0。没有布局动画师,一切正常,支持 lib < 25。

RecyclerView抛出异常,因为holder.itemView.getparent()不为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));
}

还有其他人经历过这种行为吗?

最佳答案

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

adapter.setHasStableIds(true);

解释:当您调用 adapter.notifyDataSetChanged();

时出现问题

recyclerView 然后调用 detachAndScrapAttachedViews(recycler); 它暂时分离并废弃所有当前附加的 subview 。 View 将被废弃到给定的 Recycler 中。 Recycler 可能更愿意重用废料 View 。

然后 scrapOrRecycleView(recycler, (int) position, (View) child); 被调用。此函数检查“hasStableIds”是真还是假。如果它是假的,那么你会得到以下错误:

"Scrapped or attached views may not be recycled."

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

如果项目 ID 是稳定的,那么它可以被 View 重用,即“回收”,从而使调用 notifyDataSetChanged() 后重新呈现的过程变得高效。如果元素 ID 不稳定,则无法保证元素已被回收,因为无法追踪它们。

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

关于android - "Scrapped or attached views may not be recycled"因为支持 lib 25.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40263355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com