4

I have a recyclerview that's been updated from remote source ,so it is working pretty well , the only annoying thing is that when the screen is pulled for a refresh and a get request is triggered the recyclerview is updated even though nothing has changed in the adapter causing the screen to blinks and that's annoying.

i tried messing with the diffutils but no result achieved

in my main fragment:

viewModel.fetchTopHeadline()

followed by an observer

 topHeadline.observe(this@FeedFragment, Observer {
        if(it == null) return@Observer

        topHeadlineAdapter.submitList(it)

    })

my Adapter:

class TopHeadlineAdapter:ListAdapter

and finally my diffutilCallback class :

class TopHeadlineDiffCallback :DiffUtil.ItemCallback<Article>(){
    override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
        return oldItem == newItem
    }

}
joghm
  • 569
  • 3
  • 20
  • What does the `Article` class look like? Does it override the `equals()` method? – gpunto Oct 04 '19 at 18:30
  • just a simple data class,@Entity(tableName = "TopHeadline",indices = [Index(value = ["publishedAt"], unique = true)]) data class Article( @PrimaryKey(autoGenerate = true) val id: Int? = null, val author: String?, val content: String?, val description: String?, val publishedAt: String?, @Embedded(prefix="source_") val source: Source, val title: String?, val url: String?, val urlToImage: String? ) – joghm Oct 04 '19 at 18:35
  • 1
    If the `RecyclerView` is animating then it means that the adapter thinks that the items changed. Make sure that the objects have the same ids and exact same content – gpunto Oct 04 '19 at 18:36
  • 1
    you are right, i am handling my data from local db after downloading from remote, so it turns out id has changed, and that's caused new item id not equal to old item id – joghm Oct 05 '19 at 18:06

0 Answers0