Created
March 12, 2019 19:19
-
-
Save jesselima/4233062567a23eccbadfd6e55bfd8830 to your computer and use it in GitHub Desktop.
Check a new list has new objects before update the old list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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