AdapterDelegatesManager { @NonNull public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { AdapterDelegate<T> delegate = getDelegateForViewType(viewType); if (delegate == null) { throw new NullPointerException("No AdapterDelegate added for ViewType " + viewType); } RecyclerView.ViewHolder vh = delegate.onCreateViewHolder(parent); if (vh == null) { throw new NullPointerException("ViewHolder returned from AdapterDelegate " + delegate + " for ViewType =" + viewType + " is null!"); } return vh; } AdapterDelegatesManager();  AdapterDelegatesManager(@NonNull AdapterDelegate<T>... delegates); AdapterDelegatesManager<T> addDelegate(@NonNull AdapterDelegate<T> delegate); AdapterDelegatesManager<T> addDelegate(int viewType,
                                                  @NonNull AdapterDelegate<T> delegate); AdapterDelegatesManager<T> addDelegate(int viewType, boolean allowReplacingDelegate,
                                                  @NonNull AdapterDelegate<T> delegate); AdapterDelegatesManager<T> removeDelegate(@NonNull AdapterDelegate<T> delegate); AdapterDelegatesManager<T> removeDelegate(int viewType); int getItemViewType(@NonNull T items, int position); @NonNull RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType); void onBindViewHolder(@NonNull T items, int position,
                                 @NonNull RecyclerView.ViewHolder holder, List payloads); void onBindViewHolder(@NonNull T items, int position,
                                 @NonNull RecyclerView.ViewHolder holder); void onViewRecycled(@NonNull RecyclerView.ViewHolder holder); boolean onFailedToRecycleView(@NonNull RecyclerView.ViewHolder holder); void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder); void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder); AdapterDelegatesManager<T> setFallbackDelegate(
            @Nullable AdapterDelegate<T> fallbackDelegate); int getViewType(@NonNull AdapterDelegate<T> delegate); @Nullable AdapterDelegate<T> getDelegateForViewType(int viewType); @Nullable AdapterDelegate<T> getFallbackDelegate(); static final int FALLBACK_DELEGATE_VIEW_TYPE; }
@Test public void onCreateViewHolder() { List<Object> items = Arrays.asList(new Object(), new Object(), new Object()); SpyableAdapterDelegate<List<Object>> d0 = new SpyableAdapterDelegate<>(0); SpyableAdapterDelegate<List<Object>> d1 = new SpyableAdapterDelegate<>(1); SpyableAdapterDelegate<List<Object>> d2 = new SpyableAdapterDelegate<>(2); AdapterDelegatesManager<List<Object>> manager = new AdapterDelegatesManager<>(); manager.addDelegate(d0); manager.addDelegate(d1); manager.addDelegate(d2); RecyclerView.ViewHolder vh = manager.onCreateViewHolder(null, 0); Assert.assertSame(vh, d0.viewHolder); Assert.assertTrue(d0.onCreateViewHolderCalled); Assert.assertFalse(d1.onCreateViewHolderCalled); Assert.assertFalse(d2.onCreateViewHolderCalled); resetDelegates(d0, d1, d2); vh = manager.onCreateViewHolder(null, 1); Assert.assertSame(vh, d1.viewHolder); Assert.assertTrue(d1.onCreateViewHolderCalled); Assert.assertFalse(d0.onCreateViewHolderCalled); Assert.assertFalse(d2.onCreateViewHolderCalled); resetDelegates(d0, d1, d2); vh = manager.onCreateViewHolder(null, 2); Assert.assertSame(vh, d2.viewHolder); Assert.assertTrue(d2.onCreateViewHolderCalled); Assert.assertFalse(d0.onCreateViewHolderCalled); Assert.assertFalse(d1.onCreateViewHolderCalled); }
@Test(expected = NullPointerException.class) public void testNoDelegates() { AdapterDelegatesManager<Object> delegatesManager = new AdapterDelegatesManager<>(); delegatesManager.onCreateViewHolder(null, 1); }