ConcatAdapter

public final class ConcatAdapter
extends Adapter<RecyclerView.ViewHolder>

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>
     ↳ androidx.recyclerview.widget.ConcatAdapter


An RecyclerView.Adapter implementation that presents the contents of multiple adapters in sequence.

 MyAdapter adapter1 = ...;
 AnotherAdapter adapter2 = ...;
 ConcatAdapter concatenated = new ConcatAdapter(adapter1, adapter2);
 recyclerView.setAdapter(concatenated);
 

By default, ConcatAdapter isolates view types of nested adapters from each other such that it will change the view type before reporting it back to the RecyclerView to avoid any conflicts between the view types of added adapters. This also means each added adapter will have its own isolated pool of RecyclerView.ViewHolders, with no re-use in between added adapters.

If your RecyclerView.Adapters share the same view types, and can support sharing RecyclerView.ViewHolder s between added adapters, provide an instance of ConcatAdapter.Config where you set ConcatAdapter.Config.isolateViewTypes to false. A common usage pattern for this is to return the R.layout.<layout_name> from the RecyclerView.Adapter.getItemViewType(int) method.

When an added adapter calls one of the notify methods, ConcatAdapter properly offsets values before reporting it back to the RecyclerView. If an adapter calls RecyclerView.Adapter.notifyDataSetChanged(), ConcatAdapter also calls RecyclerView.Adapter.notifyDataSetChanged() as calling RecyclerView.Adapter.notifyItemRangeChanged(int, int) will confuse the RecyclerView. You are highly encouraged to to use SortedList or ListAdapter to avoid calling RecyclerView.Adapter.notifyDataSetChanged().

Whether ConcatAdapter should support stable ids is defined in the ConcatAdapter.Config object. Calling RecyclerView.Adapter.setHasStableIds(boolean) has no effect. See documentation for ConcatAdapter.Config.StableIdMode for details on how to configure ConcatAdapter to use stable ids. By default, it will not use stable ids and sub adapter stable ids will be ignored. Similar to the case above, you are highly encouraged to use ListAdapter, which will automatically calculate the changes in the data set for you so you won't need stable ids.

It is common to find the adapter position of a RecyclerView.ViewHolder to handle user action on the RecyclerView.ViewHolder. For those cases, instead of calling RecyclerView.ViewHolder.getAdapterPosition(), use RecyclerView.ViewHolder.getBindingAdapterPosition(). If your adapters share RecyclerView.ViewHolders, you can use the RecyclerView.ViewHolder.getBindingAdapter() method to find the adapter which last bound that RecyclerView.ViewHolder.

Summary

Nested classes

class ConcatAdapter.Config

The configuration object for a ConcatAdapter

Public constructors

ConcatAdapter(Adapter...<? extends RecyclerView.ViewHolder> adapters)

Creates a ConcatAdapter with ConcatAdapter.Config.DEFAULT and the given adapters in the given order.

ConcatAdapter(ConcatAdapter.Config config, Adapter...<? extends RecyclerView.ViewHolder> adapters)

Creates a ConcatAdapter with the given config and the given adapters in the given order.

ConcatAdapter(List<? extends Adapter<? extends RecyclerView.ViewHolder>> adapters)

Creates a ConcatAdapter with ConcatAdapter.Config.DEFAULT and the given adapters in the given order.

ConcatAdapter(ConcatAdapter.Config config, List<? extends Adapter<? extends RecyclerView.ViewHolder>> adapters)

Creates a ConcatAdapter with the given config and the given adapters in the given order.

Public methods

boolean addAdapter(Adapter<? extends RecyclerView.ViewHolder> adapter)

Appends the given adapter to the existing list of adapters and notifies the observers of this ConcatAdapter.

boolean addAdapter(int index, Adapter<? extends RecyclerView.ViewHolder> adapter)

Adds the given adapter to the given index among other adapters that are already added.

int findRelativeAdapterPositionIn(Adapter<? extends RecyclerView.ViewHolder> adapter, RecyclerView.ViewHolder viewHolder, int localPosition)

Returns the position of the given RecyclerView.ViewHolder in the given RecyclerView.Adapter.

List<? extends Adapter<? extends RecyclerView.ViewHolder>> getAdapters()

Returns an unmodifiable copy of the list of adapters in this ConcatAdapter.

int getItemCount()

Returns the total number of items in the data set held by the adapter.

long getItemId(int position)

Return the stable ID for the item at position.

int getItemViewType(int position)

Return the view type of the item at position for the purposes of view recycling.

void onAttachedToRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

void onBindViewHolder(RecyclerView.ViewHolder holder, int position)

Called by RecyclerView to display the data at the specified position.

RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

void onDetachedFromRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it stops observing this Adapter.

boolean onFailedToRecycleView(RecyclerView.ViewHolder holder)

Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state.

void onViewAttachedToWindow(RecyclerView.ViewHolder holder)

Called when a view created by this adapter has been attached to a window.

void onViewDetachedFromWindow(RecyclerView.ViewHolder holder)

Called when a view created by this adapter has been detached from its window.

void onViewRecycled(RecyclerView.ViewHolder holder)

Called when a view created by this adapter has been recycled.

boolean removeAdapter(Adapter<? extends RecyclerView.ViewHolder> adapter)

Removes the given adapter from the adapters list if it exists

void setHasStableIds(boolean hasStableIds)

Calling this method is an error and will result in an UnsupportedOperationException.

void setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy strategy)

Calling this method is an error and will result in an UnsupportedOperationException.

Inherited methods

Public constructors

ConcatAdapter

public ConcatAdapter (Adapter...<? extends RecyclerView.ViewHolder> adapters)

Creates a ConcatAdapter with ConcatAdapter.Config.DEFAULT and the given adapters in the given order.

Parameters
adapters Adapter: The list of adapters to add

ConcatAdapter

public ConcatAdapter (ConcatAdapter.Config config, 
                Adapter...<? extends RecyclerView.ViewHolder> adapters)

Creates a ConcatAdapter with the given config and the given adapters in the given order.

Parameters
config ConcatAdapter.Config