https://unsplash.com/photos/zNRITe8NPqY

Android Paging 3 Library — How to Update an Item in the List

Jermaine Dilao

--

Hi! I’m writing this short blog to share a simple problem that I encountered while implementing Paging 3 in one of my projects.

Let’s get started!

Problem

Let’s say we are developing a Notifications screen that requires pagination. Our item has 2 states, read and unread state. Currently, you can’t update an item directly from PagingData as exposed by the Paging 3 library.

Workaround

As a simple workaround, we can make the read state inside our Notification object mutable like this:

data class Notification(
val id: Int,
var read: Boolean <-- Emphasis on the "var"
...
)

With this setup, we can update the state of the item in our adapter like this:

class NotificationsAdapter(...) : PagingDataAdapter(...) {

...

fun markItemAsRead(position: Int) {
snapshot()[position].read = true
notifyItemChanged(position)
}
}

And that’s it!

Note that this only applies to such simple cases. Let’s solve more complicated limitations of Paging 3 Library when we get there!

I hope this short blog will help someone out there!

Happy coding!

--

--

Jermaine Dilao

A Work in Progress Android Developer. You can check me @ https://jermainedilao.github.io/. | 💻 Senior Android Developer @ Speakap