/** * Updates the adapter with a new list that will be diffed on the <em>main</em> thread * and displayed once diff results are calculated. Not recommended for huge lists. * @param newGroups List of {@link Group} */ @SuppressWarnings("unused") public void update(@NonNull final Collection<? extends Group> newGroups) { final List<Group> oldGroups = new ArrayList<>(groups); final int oldBodyItemCount = getItemCount(oldGroups); final int newBodyItemCount = getItemCount(newGroups); final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff( new DiffCallback(oldBodyItemCount, newBodyItemCount, oldGroups, newGroups)); for (Group group : groups) { group.unregisterGroupDataObserver(this); } groups.clear(); groups.addAll(newGroups); for (Group group : newGroups) { group.registerGroupDataObserver(this); } diffResult.dispatchUpdatesTo(diffUtilCallbacks); }
/** * Updates the adapter with a new list that will be diffed on a background thread * and displayed once diff results are calculated. * * NOTE: This update method is NOT compatible with partial updates (change notifications * driven by individual groups and items). If you update using this method, all partial * updates will no longer work and you must use this method to update exclusively. * * @param newGroups List of {@link Group} */ @SuppressWarnings("unused") public void updateAsync(@NonNull final List<? extends Group> newGroups) { final List<Group> oldGroups = new ArrayList<>(groups); final int oldBodyItemCount = getItemCount(oldGroups); final int newBodyItemCount = getItemCount(newGroups); asyncDiffUtil.calculateDiff(newGroups, new DiffCallback(oldBodyItemCount, newBodyItemCount, oldGroups, newGroups)); }