androidandroid-recyclerviewandroid-jetpackandroid-pagedlistview

How to update a single item in a PagedListAdapter


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?


Solution

  • 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()