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 ViewHolder
s, with no re-use in between added adapters.
If your Adapter
s 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 ViewHolder
s, you can use the ViewHolder#getBindingAdapter()
method to find the adapter which last bound that ViewHolder
.
Summary
Nested classes | |
---|---|
The configuration object for a |
Public constructors | |
---|---|
<init>(@NonNull vararg adapters: RecyclerView.Adapter<out RecyclerView.ViewHolder!>!) Creates a ConcatAdapter with |
|
<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. |
|
<init>(@NonNull adapters: MutableList<out RecyclerView.Adapter<out RecyclerView.ViewHolder!>!>) Creates a ConcatAdapter with |
|
<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 |
addAdapter(@NonNull adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder!>) Appends the given adapter to the existing list of adapters and notifies the observers of this |
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, |