gpt4 book ai didi

android - 什么时候为 recyclerView 使用 hasStableIds true?

转载 作者:行者123 更新时间:2023-12-03 17:13:52 25 4
gpt4 key购买 nike

我目前正在玩hasStableIds回收商的观点,我有以下观察结果:

1) 当hasStableIds = false :

  • 一旦notifyDataSetChanged()调用,它将使所有 View 都变脏并再次回收它们,因此onCreateViewHolder()onBindViewHolder()将为每个项目调用。

  • 2) 当 hasStableIds = true :
  • 一旦notifyDataSetChanged()调用时,它会检查 ids,然后决定是否为 recyclerView 项目创建一个 viewHolder。如果数据集没有改变,那么它只会调用 onBindViewHolder()不是 onCreateViewHolder()再次。

  • 因此,根据上述行为,我很困惑我们是否应该始终将我们的 recyclerview 与 hasStableIds = true 一起使用。这样它就可以避免再次创建 View ?或者我们应该在什么特定条件下使用它?

    最佳答案

    根据文档Enables adapter publishes a unique Id/value that can act as a key for the item at a given position in recyclerView.如果该项目在数据集中重新定位,则为该项目返回的 ID 应该相同。
    这使您可以避免在适配器通知更改或执行任何位置操作时突然闪烁,
    警告! ,除非您知道列表项/内容在更新期间不会更改,否则不建议这样做。
    Android hasStableIds

    关于android - 什么时候为 recyclerView 使用 hasStableIds true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426211/

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