Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmitry-bystrov/7fa57868b3e0cc1c95624aa285aec7eb to your computer and use it in GitHub Desktop.
Save dmitry-bystrov/7fa57868b3e0cc1c95624aa285aec7eb to your computer and use it in GitHub Desktop.
public void addPhotoToList(String path) {
if (photoInfoList.size() > 0
&& photoInfoList.get(photoInfoList.size() - 1).getImageFilePath().equals(path)) {
// может получиться так, что новая фотография уже
// попала в список во время пересоздания активити
// в таком случае повторно добавлять её туда не нужно
return;
}
if (photoInfoList.size() == 0) {
photoInfoList.add(new PhotoInfo(path));
notifyDataSetChanged();
} else {
List<PhotoInfo> newPhotoInfoList = new ArrayList<>(photoInfoList);
newPhotoInfoList.add(new PhotoInfo(path));
dispatchUpdates(newPhotoInfoList);
}
}
public void deletePhotoFromList(int position) {
List<PhotoInfo> newPhotoInfoList = new ArrayList<>(photoInfoList);
newPhotoInfoList.remove(position);
dispatchUpdates(newPhotoInfoList);
}
private void dispatchUpdates(List<PhotoInfo> newPhotoInfoList) {
DiffUtilCallback diffUtilCallback =
new DiffUtilCallback(photoInfoList, newPhotoInfoList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUtilCallback);
photoInfoList = newPhotoInfoList;
diffResult.dispatchUpdatesTo(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment