Google is committed to advancing racial equity for Black communities. See how.

ConcatAdapter

class ConcatAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder!>
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>
   ↳ androidx.recyclerview.widget.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 Config#isolateViewTypes to false. A common usage pattern for this is to return the R.layout.<layout_name> from the 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 Adapter#notifyDataSetChanged(), ConcatAdapter also calls Adapter#notifyDataSetChanged() as calling Adapter#notifyItemRangeChanged(int, int) will confuse the RecyclerView. You are highly encouraged to to use SortedList or ListAdapter to avoid calling Adapter#notifyDataSetChanged().

Whether ConcatAdapter should support stable ids is defined in the Config object. Calling Adapter#setHasStableIds(boolean) 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 ViewHolder#getAdapterPosition(), use ViewHolder#getBindingAdapterPosition(). If your adapters share ViewHolders, you can use the ViewHolder#getBindingAdapter() method to find the adapter which last bound that ViewHolder.

Summary

Nested classes

The configuration object for a ConcatAdapter.

Public constructors
<init>(@NonNull vararg adapters: RecyclerView.Adapter<out RecyclerView.ViewHolder!>!)

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

<init>(@NonNull config: ConcatAdapter.Config, @NonNull vararg adapters: RecyclerView.Adapter<out RecyclerView.ViewHolder!>!)

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

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

<init>(@NonNull config: ConcatAdapter.Config, @NonNull adapters: MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>)

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

Public methods
Boolean

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

Boolean
addAdapter(index: Int, @NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>)

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

Int
findRelativeAdapterPositionIn(@NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>, @NonNull viewHolder: RecyclerView.ViewHolder, globalPosition: Int)

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

MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>

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

Int

Long
getItemId(position: Int)

Int
getItemViewType(position: Int)

Unit
onAttachedToRecyclerView(@NonNull recyclerView: RecyclerView)

Unit
onBindViewHolder(@NonNull holder: RecyclerView.ViewHolder, position: Int)

RecyclerView.ViewHolder
onCreateViewHolder(@NonNull parent: ViewGroup, viewType: Int)

Unit
onDetachedFromRecyclerView(@NonNull recyclerView: RecyclerView)

Boolean

Unit

Unit

Unit

Boolean

Removes the given adapter from the adapters list if it exists

Unit
setHasStableIds(hasStableIds: Boolean)

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

Unit

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

Inherited functions

Public constructors

<init>

ConcatAdapter(@NonNull vararg adapters: RecyclerView.Adapter<out RecyclerView.ViewHolder!>!)

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

Parameters
adapters RecyclerView.Adapter<out RecyclerView.ViewHolder!>!: The list of adapters to add

<init>

ConcatAdapter(
    @NonNull config: ConcatAdapter.Config,
    @NonNull vararg adapters: RecyclerView.Adapter<out RecyclerView.ViewHolder!>!)

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

Parameters
config ConcatAdapter.Config: The configuration for this ConcatAdapter
adapters RecyclerView.Adapter<out RecyclerView.ViewHolder!>!: The list of adapters to add

See Also

<init>

ConcatAdapter(@NonNull adapters: MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>)

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

Parameters
adapters MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>: The list of adapters to add

<init>

ConcatAdapter(
    @NonNull config: ConcatAdapter.Config,
    @NonNull adapters: MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>)

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

Parameters
config ConcatAdapter.Config: The configuration for this ConcatAdapter
adapters MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>: The list of adapters to add

See Also

Public methods

addAdapter

fun addAdapter(@NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>): Boolean

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

Parameters
adapter RecyclerView.Adapter<out RecyclerView.ViewHolder!>: The new adapter to add
Return
Boolean true if the adapter is successfully added because it did not already exist, false otherwise.

addAdapter

fun addAdapter(
    index: Int,
    @NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>
): Boolean

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

Parameters
index Int: The index into which to insert the adapter. ConcatAdapter will throw an IndexOutOfBoundsException if the index is not between 0 and current adapter count (inclusive).
adapter RecyclerView.Adapter<out RecyclerView.ViewHolder!>: The new adapter to add to the adapters list.
Return
Boolean true if the adapter is successfully added because it did not already exist, false otherwise.

findRelativeAdapterPositionIn

fun findRelativeAdapterPositionIn(
    @NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>,
    @NonNull viewHolder: RecyclerView.ViewHolder,
    globalPosition: Int
): Int

Returns the position of the given ViewHolder in the given Adapter. If the given Adapter is not part of this ConcatAdapter, RecyclerView#NO_POSITION is returned.

Parameters
adapter RecyclerView.Adapter<out RecyclerView.ViewHolder!>: The adapter which is a sub adapter of this ConcatAdapter or itself.
viewHolder RecyclerView.ViewHolder: The view holder whose local position in the given adapter will be returned.
Return
Int The local position of the given ViewHolder in the given Adapter or RecyclerView#NO_POSITION if the ViewHolder is not bound to an item or the given Adapter is not part of this ConcatAdapter.

getAdapters

@NonNull fun getAdapters(): MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>

Returns an unmodifiable copy of the list of adapters in this ConcatAdapter. Note that this is a copy hence future changes in the ConcatAdapter are not reflected in this list.

Return
MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!> A copy of the list of adapters in this ConcatAdapter.

getItemCount

fun getItemCount(): Int

getItemId

fun getItemId(position: Int): Long

getItemViewType

fun getItemViewType(position: Int): Int

onAttachedToRecyclerView

fun onAttachedToRecyclerView(@NonNull recyclerView: RecyclerView): Unit

onBindViewHolder

fun onBindViewHolder(
    @NonNull holder: RecyclerView.ViewHolder,
    position: Int
): Unit

onCreateViewHolder

@NonNull fun onCreateViewHolder(
    @NonNull parent: ViewGroup,
    viewType: Int
): RecyclerView.ViewHolder

onDetachedFromRecyclerView

fun onDetachedFromRecyclerView(@NonNull recyclerView: RecyclerView): Unit

onFailedToRecycleView

fun onFailedToRecycleView(@NonNull holder: RecyclerView.ViewHolder): Boolean

onViewAttachedToWindow

fun onViewAttachedToWindow(@NonNull holder: RecyclerView.ViewHolder): Unit

onViewDetachedFromWindow

fun onViewDetachedFromWindow(@NonNull holder: RecyclerView.ViewHolder): Unit

onViewRecycled

fun onViewRecycled(@NonNull holder: RecyclerView.ViewHolder): Unit

removeAdapter

fun removeAdapter(@NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>): Boolean

Removes the given adapter from the adapters list if it exists

Parameters
adapter RecyclerView.Adapter<out RecyclerView.ViewHolder!>: The adapter to remove
Return
Boolean true if the adapter was previously added to this ConcatAdapter and now removed or false if it couldn't be found.

setHasStableIds

fun setHasStableIds(hasStableIds: Boolean): Unit

Calling this method is an error and will result in an UnsupportedOperationException. You should use the Config object passed into the ConcatAdapter to configure this behavior.

Parameters
hasStableIds Boolean: Whether items in data set have unique identifiers or not.

setStateRestorationPolicy

fun setStateRestorationPolicy(@NonNull strategy: RecyclerView.Adapter.StateRestorationPolicy): Unit

Calling this method is an error and will result in an UnsupportedOperationException. ConcatAdapter infers this value from added Adapters.

Parameters
strategy RecyclerView.Adapter.StateRestorationPolicy: The saved state restoration strategy for this Adapter such that ConcatAdapter will allow state restoration only if all added adapters allow it or there are no adapters.