且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Android Paging组件中的PagedListAdapter中删除项目

更新时间:2023-12-05 18:20:34

根据官方文档


如果您有更精细的更新信号,例如网络API如果要通知列表中的单个项目更新,建议将数据从网络加载到内存中。然后通过包装内存中快照的数据源将数据提供给PagedList。每次内存中的副本更改时,都将使先前的数据源无效,然后可以创建一个新的包装快照的新状态。

If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.

PagedList 是不可变的,因此您无法对其进行任何修改。您需要做的是:

PagedList is immutable so you can't do any modifications against it. What you need to do is:


  1. 维护一个可变列表 L 存储服务器您的自定义 DataSource 中的响应。

  2. 将其传递给 LoadInitialCallback.onResult() PagedList 现在由 L 支持)。

  3. 执行任何操作您想要 L

  4. 调用 DataSource.invalidate()进行配对使用新的数据源,因此调用 DataSource.loadInitial()来反映步骤3中的更改。

  1. maintain a mutable list L storing your server responses in your custom DataSource.
  2. pass it to LoadInitialCallback.onResult() (the PagedList is backing by L now).
  3. do whatever you like to L.
  4. call DataSource.invalidate() to pair with a new data source, so DataSource.loadInitial() is called to reflect the changes in step 3.