如何从Android Paging组件中的PagedListAdapter中删除项目 [英] How to remove an item from PagedListAdapter in Android Paging Component

查看:861
本文介绍了如何从Android Paging组件中的PagedListAdapter中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有翻新功能的分页从REST API加载通知数据列表。

I used Paging with Retrofit to loading list of Notifications data from REST API.

当我按下通知项的删除按钮时,我会调用Delete API将其删除。删除API响应成功后,从 PagedListAdapter 删除它的正确方法是什么? PagedList PagedListAdapter 不支持按索引或对象删除项目。

When I press delete button of a notification item then I call delete API to remove it. What is the proper way to remove it from PagedListAdapter after delete API response success? PagedList or PagedListAdapter does not support remove the item by index or object.

我尝试调用 DataSource.validate(),但它是从开始而不是从当前页面重新加载的。 / p>

I tried to call DataSource.validate() but it reloads from begin, not from the current page.

推荐答案

根据官方文档


如果您有更精细的更新信号,例如网络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.

这篇关于如何从Android Paging组件中的PagedListAdapter中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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