public final class ConcatAdapter
extends Adapter<RecyclerView.ViewHolder>

   ↳ 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);

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.


Nested classes

class ConcatAdapter.Config

The configuration object for a ConcatAdapter

Public constructors