https://unsplash.com/photos/zNRITe8NPqY

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

1 min readOct 8, 2020

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!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jermaine Dilao

Written by Jermaine Dilao

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

Responses (7)

Write a response

Thank you for this! This whole paging library is a mess, so hard to update items

3

Can we add new item in list before sending it to datasource ? In example, I got a bunch of list with createdAt property, and I want to add new item to group the list with loop. then I send it to datasource.

1

How to delete item?

1