Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created March 12, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesselima/4233062567a23eccbadfd6e55bfd8830 to your computer and use it in GitHub Desktop.
Save jesselima/4233062567a23eccbadfd6e55bfd8830 to your computer and use it in GitHub Desktop.
Check a new list has new objects before update the old list.
package com.tetraandroid.diffutilexample.helper;
import android.support.v7.util.DiffUtil;
import com.tetraandroid.diffutilexample.http.apimodel.Result;
import java.util.List;
public class MyDiffUtil extends DiffUtil.Callback {
private List<Result> oldList;
private List<Result> newList;
public MyDiffUtil(List<Result> oldList, List<Result> newList) {
this.oldList = oldList;
this.newList = newList;
}
@Override
public int getOldListSize() {
return oldList.size();
}
@Override
public int getNewListSize() {
return newList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).id == newList.get(newItemPosition).id;
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment