3

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?

Luciano
  • 2,278
  • 4
  • 31
  • 55

2 Answers2

4

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)  
}
Jhonatan Sabadi
  • 690
  • 5
  • 13
1

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()
Luciano
  • 2,278
  • 4
  • 31
  • 55
  • but how to actually update the contents of PagedList? – Raghav Satyadev Mar 11 '19 at 14:45
  • @RaghavSatyadev what do you mean? In my situation my list item had a toggle button. Clicking the button will trigger a state change on the server. Calling notifyItemChanged will fetch the updated state from the server and onBind will be called inside the recyclerView to show the new state. – Luciano Mar 12 '19 at 09:18
  • 2
    it will reload the whole list, imagine that user is on 100th item then invalidating the list will throw him on 1st item and whole progress will be lost. also adapter.notifyItemChanged(selectedItemPosition) won't work as there is no option to just change one item in local list, we have to fetch the whole list again from the app – Raghav Satyadev Mar 12 '19 at 13:23