Skip to content

Instantly share code, notes, and snippets.

@rogergcc
Last active October 2, 2023 00:34
Show Gist options
  • Save rogergcc/dfbd35cd74ebf7bb7052f1d2cbb1a592 to your computer and use it in GitHub Desktop.
Save rogergcc/dfbd35cd74ebf7bb7052f1d2cbb1a592 to your computer and use it in GitHub Desktop.
DiffUtil
//In Adapter
public void setContactsList(final List<Contact> contactsList) {
Log.d("LOGG_DEBUG", "setNotes COntactAdapter: ");
ContactsDiffUtilCallback contactsDiffUtilCallback = new ContactsDiffUtilCallback(mContactList, contactsList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(contactsDiffUtilCallback);
diffResult.dispatchUpdatesTo(this);
mContactList.clear();
mContactList.addAll(contactsList);
}
package com.rogergcc.workplaycontacts.ui.utils;
import androidx.recyclerview.widget.DiffUtil;
import com.rogergcc.workplaycontacts.data.local.db.entity.Contact;
import java.util.List;
/**
* Created on septiembre.
* year 2023 .
*/
public class ContactsDiffUtilCallback extends DiffUtil.Callback {
private final List<Contact> mOldList;
private final List<Contact> mNewList;
public ContactsDiffUtilCallback(List<Contact> oldItems, List<Contact> newItems) {
this.mOldList = oldItems;
this.mNewList = newItems;
}
@Override
public int getOldListSize() {
return mOldList != null ? mOldList.size() : 0;
}
@Override
public int getNewListSize() {
return mNewList != null ? mNewList.size() : 0;
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mNewList.get(newItemPosition).getId() == mOldList.get(oldItemPosition).getId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return mNewList.get(newItemPosition).equals(mOldList.get(oldItemPosition));
}
// @Nullable
// @Override
// public Object getChangePayload(int oldItemPosition, int newItemPosition) {
// return mNewList.get(newItemPosition).equals(mOldList.get(oldItemPosition));
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment