How to update a single item in a PagedListAdapter

4.7k Views Asked by At

I've implemented the new PagedListAdapter with an ItemKeyedDataSource. My list contains feed items with a like button. A click on the like button should refresh the item.

To refresh my list item on a like button click, i update the state in my Firestore back-end, and call invalidate on my ItemKeyedDataSource. But this will refresh my whole list while jumping back to the top of my list.

I wasn't sure if this is normal behaviour, so i went debugging and checking maiby my DiffUtil.ItemCallback was the problem. But the two methods (areItemsTheSame and areContentsTheSame) are never being called.

So is calling invalidate on the DataSource the way to go to trigger an update of a single item? And will the DiffUtil help me keep track of my position in the list after an update, or am i looking in the wrong direction?

2

There are 2 best solutions below

3
Luciano On BEST ANSWER

After some digging around, i found the following solution:

To update a single item

You can call notifyItemChanged on the adapter. This will fetch the update from the datasource and updates the list item.

adapter.notifyItemChanged(selectedItemPosition)

To refresh all data

You can call invalidate on the DataSource

feedDataSourceFactory.feedLiveData.value!!.invalidate()
0
Jhonatan Sabadi On

You can modify like normal adapter, the only difference, is that you gonna use getItem(position)

fun changeFavState(position: Int, isFavorite: Boolean) {  
  getItem(position)?.let {  
  it.isFavorite = isFavorite  
  }  
  notifyItemChanged(position)  
}