Using DiffUtil in Recyclerview Android
Posted By : Ajit Jati | 04-Dec-2017
Recyclerview in android is the most commonly used component
Updating data via
-
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 andrecyclerview 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.
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!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.