如何使用 Paging 3 库更新单个项目 [英] How to update single item using Paging 3 library

查看:34
本文介绍了如何使用 Paging 3 库更新单个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Paging 3 库中的 PagingAdapter 找到一种方法来更新回收器视图中的单个项目.我只找到了一种使用 PagingAdapter.refresh() 方法的方法.但是这种方法强制从网络加载所有列表.有人知道如何在不从网络加载所有页面的情况下实现它吗?

解决方案

目前,更新后备数据集的唯一方法是使列表无效并重新加载.对于使用缓存层(在数据库中,例如房间或内存中)的分层源,这通常是一个可接受的廉价选项,尽管正在进行工作以支持更细粒度的更新(请参阅 https://issuetracker.google.com/160232968).

就目前的分层源而言,您需要将网络调用移动到 RemoteMediator 中,您可以在 Pager 的构造函数中注册它,并缓存您的网络获取到像 Room 这样的数据库(它可以为您生成一个 PagingSource 实现),或者自己编写一个内存中的实现.

codelabDAC 文档 是一个很好的资源,并且有代码示例来指导您!>

I am trying to find a way to update single item in recycler view using PagingAdapter from Paging 3 library. I have found only one way with PagingAdapter.refresh() method. But this method force to load all list from network. Does anybody know how to implement it without loading all pages from network?

解决方案

Currently, the only way to update the backing dataset is to invalidate and reload the list. This is generally an acceptably cheap option for layered sources that use a cached layer (either in db such as room or in memory), although there is ongoing work to support more granular updates (see https://issuetracker.google.com/160232968).

In terms of layered source for now, you'll need to move your network calls into a RemoteMediator which you can register in Pager's constructor, and cache your network fetches into either a DB like with Room (which can generate a PagingSource implementation for you), or write an in-memory one yourself.

The codelab and DAC docs are a great resource for this, and have code samples to guide you!

这篇关于如何使用 Paging 3 库更新单个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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