1

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.

Martin Mbae
  • 1,108
  • 1
  • 16
  • 27

1 Answers1

1

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.

dlam
  • 3,547
  • 17
  • 20