public GroupAdapterViewTypeDelegate(List<AdapterViewTypeDelegate> delegates) { childAdapterViewTypeDelegates.addAll(delegates); int childViewType = 0; for (AdapterViewTypeDelegate delegate : childAdapterViewTypeDelegates) { if (delegate.getViewType() != RecyclerView.INVALID_TYPE) { throw new IllegalStateException("Do not reuse AdapterViewTypeDelegate instances"); } delegate.setViewType(childViewType); childViewType++; delegate.addFeatureEventListener(this::fireEvent); childDiffUtilComparators.add(delegate.createDiffUtilComparator()); childViewHolderCache.add(new ArrayDeque<>()); } }
private void registerAdapterViewTypeDelegates(List<FeatureController<MODEL>> featureControllers) { for (FeatureController<MODEL> featureController : featureControllers) { for (AdapterViewTypeDelegate delegate : featureController.getAdapterViewTypeDelegates()) { // assign unique view type delegate.setViewType(mapViewTypeToAdapterViewTypeDelegate.size()); // register delegate mapViewTypeToAdapterViewTypeDelegate.put(delegate.getViewType(), delegate); // register item comparator mapViewTypeToItemComparator.put( delegate.getViewType(), delegate.createDiffUtilComparator()); } } }
private ViewItem<ChildModel> createViewItem(ChildModel model, int viewType) { AdapterViewTypeDelegate tempDelegate = new StubAdapterViewTypeDelegate(); tempDelegate.setViewType(viewType); return new ViewItem<>(model, tempDelegate); }
private AdapterViewTypeDelegate createValidAdapterViewTypeDelegate(int expectedViewType) { AdapterViewTypeDelegate delegate = createMock(AdapterViewTypeDelegate.class); expect(delegate.getViewType()).andStubReturn(RecyclerView.INVALID_TYPE); delegate.setViewType(expectedViewType); delegate.addFeatureEventListener(anyObject()); // we build a real DiffUtilComparator so we can generate realistic DiffUtil.DiffResult expect(delegate.createDiffUtilComparator()).andStubReturn(new DiffUtilComparator<ChildModel>() { @Override public boolean areItemsTheSame(ChildModel oldModel, ChildModel newModel) { return oldModel.key == newModel.key; } @Override public boolean areContentsTheSame(ChildModel oldModel, ChildModel newModel) { return oldModel.value.equals(newModel.value); } @Override public Object getChangePayload(ChildModel oldModel, ChildModel newModel) { return null; } }); return delegate; }