How to insert item at a position using android Paging 3

Viewed 1413

I am loading items from API using the Paging 3 library. I would like to add an item to a specific position in the recyclerview.

If I am using normal adapter I would just use

mArrayList.add(position, item);
notifyItemInserted(position); 

But I am using the PagingDataAdapter and I have tried this

val newPostResponse = response?.responseBody
homeAdapter.snapshot().items.toMutableList().add(0, newPostResponse)
homeAdapter.notifyItemInserted(0)

I have also tried

val newPostResponse = response?.responseBody
homeAdapter.snapshot().items.toMutableList().add(0, newPostResponse)
homeAdapter.notifyDataSetChanged()

None of these is working. Someone help me know the right way to do it.

1 Answers

This seems to be a duplicate of How to update single item using Paging 3 library.

You must go through the .invalidate() loop to maintain a single source of truth and make Paging aware of your local changes. This is currently the only supported way to add an item to Paging.

Related