androidandroid-recyclerviewkotlin-flowandroid-listadapter

Android - How to make the recyclerview ListAdpater stop blinking?


I have a recycler view whose adapter uses ListAdapter (version 1.1.0) :

class InnerEpisodeFragmentAdapter(
    private val actCtx: Context,
) : ListAdapter<Episode, InnerEpisodeFragmentAdapter.MViewHolder>(COMPARATOR) {
...

The recycler view is fed by a kotlin flow coming from Room database Episode table :

vm.episodesFlow().asLiveData().observe(viewLifecycleOwner) { episodes ->
  episodes.let { adapter.submitList(it) }
}

@Query("SELECT * FROM Episode ORDER BY pubDate DESC")
fun episodesFlow(): Flow<List<Episode>>

As excepted, each time a tuple changes in Episode table, a new list of episodes is emitted and the recycler view is update.

It works fine, but with an horrible BLINK at each update. It gives a bad user experience.

How can I avoid this blinking when the flow emit new values ?

In the previous version of my app, I used functions like notifyDataSetChanged() or notifyItemChanged() which never blink.I know I could still try to use these functions but I would be very disappointed if I could not avoid the blinks when using the kotlin flow as shown above. Thanks.


Solution

  • you comparators

    areItemsThesame() is wrongly implemented. What you are comparing is reference where is old object and newObject might not same objects. Instead you some use some kind of uuid to compare the two like primary key. If objects are different it will return false and hence areContentsTheSame will never be called otherwise if objects are same their is no point of comparing contents as both oldObject and newObject are pointing to same reference.

    Sorry for errors as I am writing from Mobile