ConcatAdapter

An 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 ViewHolders, with no re-use in between added adapters.

If your Adapters share the same view types, and can support sharing ViewHolder s between added adapters, provide an instance of Config where you set isolateViewTypes to false. A common usage pattern for this is to return the R.layout.<layout_name> from the getItemViewType 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 notifyDataSetChanged, ConcatAdapter also calls notifyDataSetChanged as calling notifyItemRangeChanged will confuse the RecyclerView. You are highly encouraged to to use SortedList or ListAdapter to avoid calling notifyDataSetChanged.

Whether ConcatAdapter should support stable ids is defined in the Config object. Calling setHasStableIds has no effect. See documentation for 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 ViewHolder to handle user action on the ViewHolder. For those cases, instead of calling getAdapterPosition, use getBindingAdapterPosition. If your adapters share ViewHolders, you can use the getBindingAdapter method to find the adapter which last bound that ViewHolder.

Constructors

Link copied to clipboard
Creates a ConcatAdapter with DEFAULT and the given adapters in the given order.
Creates a ConcatAdapter with the given config and the given adapters in the given order.
constructor(@NonNull adapters: List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>>)
Creates a ConcatAdapter with DEFAULT and the given adapters in the given order.
Creates a ConcatAdapter with the given config and the given adapters in the given order.

Types

Link copied to clipboard
class Config
The configuration object for a ConcatAdapter.

Functions

Link copied to clipboard
Appends the given adapter to the existing list of adapters and notifies the observers of this ConcatAdapter.
Adds the given adapter to the given index among other adapters that are already added.
Link copied to clipboard
Returns the position of the given ViewHolder in the given Adapter.
Link copied to clipboard
Returns an unmodifiable copy of the list of adapters in this ConcatAdapter.
Link copied to clipboard
open fun getItemCount(): Int
Returns the total number of items in the data set held by the adapter.
Link copied to clipboard
open fun getItemId(position: Int): Long
Return the stable ID for the item at position.
Link copied to clipboard
open fun getItemViewType(position: Int): Int
Return the view type of the item at position for the purposes of view recycling.
Link copied to clipboard
Retrieve the adapter and local position for a given position in this ConcatAdapter.
Link copied to clipboard
Called by RecyclerView when it starts observing this Adapter.
Link copied to clipboard
open fun onBindViewHolder(@NonNull holder: RecyclerView.ViewHolder, position: Int)
Called by RecyclerView to display the data at the specified position.
Link copied to clipboard
Called when RecyclerView needs a new ViewHolder of the given type to represent an item.
Link copied to clipboard
Called by RecyclerView when it stops observing this Adapter.
Link copied to clipboard
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state.
Link copied to clipboard
Called when a view created by this adapter has been attached to a window.
Link copied to clipboard
Called when a view created by this adapter has been detached from its window.
Link copied to clipboard
Called when a view created by this adapter has been recycled.
Link copied to clipboard
Removes the given adapter from the adapters list if it exists
Link copied to clipboard
open fun setHasStableIds(hasStableIds: Boolean)
Calling this method is an error and will result in an UnsupportedOperationException.
Link copied to clipboard
Calling this method is an error and will result in an UnsupportedOperationException.