Skip to content

An example on how to implement DiffUtil for RecyclerViews in Kotlin, and what to do to make code simpler.

Notifications You must be signed in to change notification settings

harshulsingla0110/diffutil-recyclerview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DiffUtil

Intro

DiffUtil is a utility class by Google that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one.

Problem

Say, we use room database. Now we add one item to room which has previous n items. So after adding it should reflect in recycler view. Now, what recycler view does is that it renders all items.

This is a performance issue.

Solution

Solution in using DiffUtil. Now, the only added items will render. Say there were 3 previous item and then we add 3 more items, then code will only be called by these 3 new items i.e. the onBindViewHolder will be called only for these new added items.

There are 2 ways 2 implement Diff Utils:

1. Recycler View Adpater

  • We have to implement Diff Util.

2. List Adapter

  • By default Diff Util is implemented.
  • Automatically use Diff Util, execute difference in background.

Both are implemented in the code. For 1 check ProgrammingListAdapter and for 2 check ProgrammingRecyclerViewAdapter.

Happy coding 👨‍💻

Demo

About

An example on how to implement DiffUtil for RecyclerViews in Kotlin, and what to do to make code simpler.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages