Using DiffUtil in Recyclerview Android

Posted By : Ajit Jati | 04-Dec-2017

Recyclerview in android is the most commonly used component now a days.You need Recyclerview not only to show a list of data but also data is grid, stack,slider view etc. In order to post data to recycler view’s UI , you need an adapter.

 

Updating data via recyclerview’s adapter is not a rocket science but doing it smartly and having performance on top is a bit challenging.Key factors which need to be kept in mind while updating data in adapter :

  • Try to avoid updating whole list every time if there was only a few subset of your dataset has been changed.?

  • Use a layout manager with orientation to get optimal performance.?

  • Try to avoid using scrollview and recyclerview at nested.

  • Instead of creating new arraylist every time before updating the list, try using List.clear() method.

  • Use Android’s DiffUtils for better and optimal performance.?   

What is DiffUtils ?

 

 

Official statement - DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one. Basically DiffUtils does all of the complex work to calculate your data set and find the suitable adapter method to update the UI.

 
Usage - 

Create a callback class and override areItemsTheSame, areContentsTheSame and getOldListSize etc methods.

public class StudentDiffCallback extends DiffUtil.Callback {

   private final List<student> mOldStudentList;
   private final List<student> mNewStudentList;

   public HotelsDiffCallback(List<hotel> oldStudentList, List<hotel> newStudentList) {
       this.mOldStudentList = oldStudentList;
       this.mNewStudentList = newStudentList;
   }

   @Override
   public int getOldListSize() {
       return mOldStudentList.size();
   }

   @Override
   public int getNewListSize() {
       return mNewStudentList.size();
   }

   @Override
   public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
       final Student oldStudent = mOldStudentList.get(oldItemPosition);
       final Student newStudent = mNewStudentList.get(newItemPosition);

         return oldHotel.getId() == newHotel.getId();
   }

   @Override
   public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
       final Student oldStudent = mOldStudentList.get(oldItemPosition);
       final Student newStudent = mNewStudentList.get(newItemPosition);

       return oldStudent.getName().equals(newStudent.getName());
   }

   @Nullable
   @Override
   public Object getChangePayload(int oldItemPosition, int newItemPosition) {
       // Implement method if you're going to use ItemAnimator
       return super.getChangePayload(oldItemPosition, newItemPosition);
   }
}

In your adapter class :

public void updateStudentListSmartly(List<student> students) {
   final StudentDiffCallback diffCallback = new StudentDiffCallback(this.compareStudentList, students);
   final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

   this.compareStudentList.clear();
   this.compareStudentList.addAll(students);
   diffResult.dispatchUpdatesTo(this);
}

 

You are done. Now see the magic in performance in recyclerview.

Hope that helps.Cheers!

[email protected]

About Author

Author Image
Ajit Jati

Ajit is an proficient Project Manager specializing in Mobile technologies. He possesses extensive development experience in Titanium, Android, Kotlin, Roku, and PHP. Ajit has successfully delivered various projects in the OTT, AR/VR, and Travel industries. His skill set includes developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within the allocated budget. He excels in collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and effectively manage expectations. He conducts regular project status meetings, ensuring effective communication and providing updates to clients and stakeholders on project progress, risks, and issues. Furthermore, Ajit takes on the role of a coach and mentor for team,offering guidance on project management best practices and assisting in their skill development.

Request for Proposal

Name is required

Comment is required

Sending message..