@Override public int getItemCount() { return getItemCount(groups); }
/** * 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)); }
public void add(@NonNull Group group) { if (group == null) throw new RuntimeException("Group cannot be null"); int itemCountBeforeGroup = getItemCount(); group.registerGroupDataObserver(this); groups.add(group); notifyItemRangeInserted(itemCountBeforeGroup, group.getItemCount()); }
public CarouselGroup(RecyclerView.ItemDecoration itemDecoration, GroupAdapter adapter) { this.adapter = adapter; carouselItem = new CarouselItem(itemDecoration, adapter); isEmpty = adapter.getItemCount() == 0; adapter.registerAdapterDataObserver(adapterDataObserver); }
/** * Adds the contents of the list of groups, in order, to the end of the adapter contents. * All groups in the list must be non-null. * * @param groups */ public void addAll(@NonNull Collection<? extends Group> groups) { if (groups.contains(null)) throw new RuntimeException("List of groups can't contain null!"); int itemCountBeforeGroup = getItemCount(); int additionalSize = 0; for (Group group : groups) { additionalSize += group.getItemCount(); group.registerGroupDataObserver(this); } this.groups.addAll(groups); notifyItemRangeInserted(itemCountBeforeGroup, additionalSize); }
/** * 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); }
public void addAllWorksWithSets() { Set<Group> groupSet = new HashSet<>(); groupSet.add(new DummyItem()); groupSet.add(new DummyItem()); groupAdapter.addAll(groupSet); Assert.assertEquals(2, groupAdapter.getItemCount()); }
/** * This idea was copied from Epoxy. :wave: Bright idea guys! * <p> * Find the model that has the given view type so we can create a viewholder for that model. * <p> * To make this efficient, we rely on the RecyclerView implementation detail that {@link * GroupAdapter#getItemViewType(int)} is called immediately before {@link * GroupAdapter#onCreateViewHolder(android.view.ViewGroup, int)}. We cache the last model * that had its view type looked up, and unless that implementation changes we expect to have a * very fast lookup for the correct model. * <p> * To be safe, we fallback to searching through all models for a view type match. This is slow and * shouldn't be needed, but is a guard against RecyclerView behavior changing. */ private Item<VH> getItemForViewType(@LayoutRes int layoutResId) { if (lastItemForViewTypeLookup != null && lastItemForViewTypeLookup.getLayout() == layoutResId) { // We expect this to be a hit 100% of the time return lastItemForViewTypeLookup; } // To be extra safe in case RecyclerView implementation details change... for (int i = 0; i < getItemCount(); i++) { Item item = getItem(i); if (item.getLayout() == layoutResId) { return item; } } throw new IllegalStateException("Could not find model for view type: " + layoutResId); }