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?
You have to fetch the data from the server and then store it into local DB using room library and then whenever you have to update any item, update the item into local DB and in turn room will reflect those changes into the UI(using LiveData). But then you have to do the paging from using local DB.
The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.
The Paging 3.0 architectural components library is a major update over the previous versions of paging library, and it is completely rewritten from the previous versions of Paging library. It has complete support for the Kotlin coroutines and other reactive streams such as RxJava and LiveData.
Unlike the previous versions of Paging library, in Paging 3.0 we have to implement a PagingSource<Key, Value> to define a data source. The PagingSource takes two parameters a Key and a Value. The Key parameter is the identifier of the data to be loaded such as page number and the Value is the type of the data itself.
The Paging Library, is a library created by Google to solve the problem of data paging on Android. A problem that we usually have to solve when creating an application is the request for long data, which does not require a single time, since the user sees only a small part of that data at a time.
If your app already uses an earlier version of the Paging library, read this page to learn more about migrating to Paging 3. If Paging 3 is the first version of the Paging library that you are using in your app, see Load and display paged data for basic usage information.
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!
Example :
fun markItemAsRead(position: Int) {
snapshot()[position].read = true
notifyItemChanged(position)
}
source : https://jermainedilao.medium.com/android-paging-3-library-how-to-update-an-item-in-the-list-52f00d9c99b2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With