I have a chat-like activity where I am using a RecyclerView with a PagedListAdaper to load a bunch of messages. I'm using a PositionalDataSource to load the data. The loading it's self works fine but when I send a message, I invalidate my datasource and the list gets remade. My problem is that it blinks when it does that:
I have tried adding setHasStableIds(true) and overriding getItemId which would work on a simple adapter but it doesn't seem to work here. I also cannot seem to be able to just add an item to the getCurrentList() because it's not supported. Also, I'm not using a database, just making requests to a server.
So my questions are, is there a better way of doing this besides invalidating the data source? Is there a way to stop the list from blinking when sending a message? Or is this library just not suited for my chat activity?
Edit:
my diff callback
private val DIFF_CALLBACK: DiffCallback<MessageModel> = object : DiffCallback<MessageModel>() {
override fun areItemsTheSame(@NonNull oldMessage: MessageModel, @NonNull newMessage: MessageModel) =
oldMessage.id == newMessage.id
override fun areContentsTheSame(@NonNull oldMessage: MessageModel, @NonNull newMessage: MessageModel) =
oldMessage.equals(newMessage)
}
Edit2 I fixed it:
So I managed to fix it by using PagedListAdapterHelper and setting it's list after the items loaded:
private var mHelper: PagedListAdapterHelper<MessageModel>? = null
init {
mHelper = PagedListAdapterHelper(this, DIFF_CALLBACK)
setHasStableIds(true)
}
fun setList(pagedList: PagedList<MessageModel>) {
pagedList.addWeakCallback(pagedList.snapshot(), object:PagedList.Callback() {
override fun onChanged(position: Int, count: Int) {
}
override fun onInserted(position: Int, count: Int) {
mHelper?.setList(pagedList)
}
override fun onRemoved(position: Int, count: Int) {
}
})
}
PagedListAdapterHelper
? – GeospherePagedListAdapterHelper
deprecated in Paging Component – Foliate