gpt4 book ai didi

android - notifyItemInserted 贵吗

转载 作者:行者123 更新时间:2023-11-29 01:29:19 26 4
gpt4 key购买 nike

我正在将我的应用程序从 sqlite 迁移到 Firebase。以前我会从数据库读取 N 项到数组列表并调用 notifyItemRangeInserted。现在从 Firebase 获取数据最方便的方式是一个一个地传递对象。我想知道是否有人会降低为每个列表项调用 notifyItemInserted 的成本。这样好还是我应该分批处理我的负载?我在 RecyclerView 中显示所有内容。

最佳答案

基于 RecyclerView 的文档:

There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

notifyItemInserted : 通知任何已注册的观察者反射(reflect)在位置的项目已被新插入

notifyItemRangeInserted :通知所有已注册的观察者,当前反射(reflect)的从 positionStart 开始的 itemCount 项已被新插入

量化和比较这两者有点棘手。

  1. 基于功能:它们似乎执行相同的操作。它们不会改变现有项目的绑定(bind),而是改变现有项目的位置。
  2. 基于notify observer后的影响 短时间调用notifyItemInserted会频繁触发已注册的observers。如果这些观察者正在做一些繁重的计算,那么每个项目的 notifyItemInserted 都会很昂贵。

但是,如果您确实有一个范围内不连续的列表(例如删除第 1、2 和 4 项),或者如果您想同时执行插入和删除,并相应地设置动画,或者您经常修改适配器的数据集,有效的方法是实现支持库中提供的 DiffUtil

Most of the time our list changes completely and we set new list to RecyclerView Adapter. And we call notifyDataSetChanged to update adapter. NotifyDataSetChanged is costly. DiffUtil class solves that problem now. It does its job perfectly

您可以找到更多信息here .

关于android - notifyItemInserted 贵吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189617/

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