-
public final class ConcatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
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
{@code false}
. A common usage pattern for this is to return the{@code R.layout.}
from the getItemViewType method.When an added adapter calls one of the
{@code 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final class
ConcatAdapter.Config
The configuration object for a ConcatAdapter.
-
Constructor Summary
Constructors Constructor Description ConcatAdapter(Array<RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with DEFAULT and the given adapters in the givenorder. ConcatAdapter(ConcatAdapter.Config config, Array<RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with the given config and the given adapters in the given order. ConcatAdapter(List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with DEFAULT and the given adapters in the givenorder. ConcatAdapter(ConcatAdapter.Config config, List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with the given config and the given adapters in the given order.
-
Method Summary
Modifier and Type Method Description boolean
addAdapter(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Appends the given adapter to the existing list of adapters and notifies the observers ofthis ConcatAdapter. boolean
addAdapter(int index, @NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Adds the given adapter to the given index among other adapters that are already added. boolean
removeAdapter(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Removes the given adapter from the adapters list if it exists int
getItemViewType(int position)
Return the view type of the item at position
for the purposesof view recycling.RecyclerView.ViewHolder
onCreateViewHolder(@NonNull() ViewGroup parent, int viewType)
Called when RecyclerView needs a new ViewHolder of the given type to representan item. void
onBindViewHolder(@NonNull() RecyclerView.ViewHolder holder, int position)
Called by RecyclerView to display the data at the specified position. void
setHasStableIds(boolean hasStableIds)
Calling this method is an error and will result in an UnsupportedOperationException. void
setStateRestorationPolicy(@NonNull() RecyclerView.Adapter.StateRestorationPolicy strategy)
Calling this method is an error and will result in an UnsupportedOperationException. long
getItemId(int position)
Return the stable ID for the item at position
.int
getItemCount()
Returns the total number of items in the data set held by the adapter. boolean
onFailedToRecycleView(@NonNull() RecyclerView.ViewHolder holder)
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycleddue to its transient state. void
onViewAttachedToWindow(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been attached to a window. void
onViewDetachedFromWindow(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been detached from its window. void
onViewRecycled(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been recycled. void
onAttachedToRecyclerView(@NonNull() RecyclerView recyclerView)
Called by RecyclerView when it starts observing this Adapter. void
onDetachedFromRecyclerView(@NonNull() RecyclerView recyclerView)
Called by RecyclerView when it stops observing this Adapter. List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>>
getAdapters()
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 inthis list. int
findRelativeAdapterPositionIn(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter, @NonNull() RecyclerView.ViewHolder viewHolder, int globalPosition)
Returns the position of the given ViewHolder in the given Adapter. -
Methods inherited from class tds.androidx.recyclerview.widget.RecyclerView.Adapter
bindViewHolder, createViewHolder, findRelativeAdapterPositionIn, getStateRestorationPolicy, hasObservers, hasStableIds, notifyDataSetChanged, notifyItemChanged, notifyItemChanged, notifyItemInserted, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, notifyItemRemoved, onBindViewHolder, onBindViewHolder, onCreateViewHolder, onFailedToRecycleView, onViewAttachedToWindow, onViewDetachedFromWindow, onViewRecycled, registerAdapterDataObserver, unregisterAdapterDataObserver
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ConcatAdapter
ConcatAdapter(Array<RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with DEFAULT and the given adapters in the givenorder.- Parameters:
adapters
- The list of adapters to add
-
ConcatAdapter
ConcatAdapter(ConcatAdapter.Config config, Array<RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with the given config and the given adapters in the given order.- Parameters:
config
- The configuration for this ConcatAdapteradapters
- The list of adapters to add
-
ConcatAdapter
ConcatAdapter(List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with DEFAULT and the given adapters in the givenorder.- Parameters:
adapters
- The list of adapters to add
-
ConcatAdapter
ConcatAdapter(ConcatAdapter.Config config, List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>> adapters)
Creates a ConcatAdapter with the given config and the given adapters in the given order.- Parameters:
config
- The configuration for this ConcatAdapteradapters
- The list of adapters to add
-
-
Method Detail
-
addAdapter
boolean addAdapter(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Appends the given adapter to the existing list of adapters and notifies the observers ofthis ConcatAdapter.
- Parameters:
adapter
- The new adapter to add
-
addAdapter
boolean addAdapter(int index, @NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Adds the given adapter to the given index among other adapters that are already added.
- Parameters:
index
- The index into which to insert the adapter.adapter
- The new adapter to add to the adapters list.
-
removeAdapter
boolean removeAdapter(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter)
Removes the given adapter from the adapters list if it exists
- Parameters:
adapter
- The adapter to remove
-
getItemViewType
int getItemViewType(int position)
Return the view type of the item at
position
for the purposesof view recycling.The default implementation of this method returns 0, making the assumption ofa single view type for the adapter. Unlike ListView adapters, types need notbe contiguous. Consider using id resources to uniquely identify item view types.
- Parameters:
position
- position to query
-
onCreateViewHolder
@NonNull() RecyclerView.ViewHolder onCreateViewHolder(@NonNull() ViewGroup parent, int viewType)
Called when RecyclerView needs a new ViewHolder of the given type to representan item.
This new ViewHolder should be constructed with a new View that can represent the itemsof the given type. You can either create a new View manually or inflate it from an XMLlayout file.
The new ViewHolder will be used to display items of the adapter using onBindViewHolder. Since it will be re-used to displaydifferent items in the data set, it is a good idea to cache references to sub views ofthe View to avoid unnecessary findViewById calls.
- Parameters:
parent
- The ViewGroup into which the new View will be added after it is bound toan adapter position.viewType
- The view type of the new View.
-
onBindViewHolder
void onBindViewHolder(@NonNull() RecyclerView.ViewHolder holder, int position)
Called by RecyclerView to display the data at the specified position. This method shouldupdate the contents of the itemView to reflect the item at the givenposition.
Note that unlike android.widget.ListView, RecyclerView will not call this methodagain if the position of the item changes in the data set unless the item itself isinvalidated or the new position cannot be determined. For this reason, you should onlyuse the
position
parameter while acquiring the related data item insidethis method and should not keep a copy of it. If you need the position of an item lateron (e.g. in a click listener), use getBindingAdapterPosition whichwill have the updated adapter position.Override onBindViewHolder instead if Adapter canhandle efficient partial bind.- Parameters:
holder
- The ViewHolder which should be updated to represent the contents of theitem at the given position in the data set.position
- The position of the item within the adapter's data set.
-
setHasStableIds
void setHasStableIds(boolean hasStableIds)
Calling this method is an error and will result in an UnsupportedOperationException.You should use the Config object passed into the ConcatAdapter to configure thisbehavior.
- Parameters:
hasStableIds
- Whether items in data set have unique identifiers or not.
-
setStateRestorationPolicy
void setStateRestorationPolicy(@NonNull() RecyclerView.Adapter.StateRestorationPolicy strategy)
Calling this method is an error and will result in an UnsupportedOperationException.ConcatAdapter infers this value from added Adapters.
- Parameters:
strategy
- The saved state restoration strategy for this Adapter such thatConcatAdapter will allow state restoration only if all addedadapters allow it orthere are no adapters.
-
getItemId
long getItemId(int position)
Return the stable ID for the item at
position
. If hasStableIds would return false this method should return NO_ID. The default implementationof this method returns NO_ID.- Parameters:
position
- Adapter position to query
-
getItemCount
int getItemCount()
Returns the total number of items in the data set held by the adapter.
-
onFailedToRecycleView
boolean onFailedToRecycleView(@NonNull() RecyclerView.ViewHolder holder)
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycleddue to its transient state. Upon receiving this callback, Adapter can clear theanimation(s) that effect the View's transient state and return
true
so thatthe View can be recycled. Keep in mind that the View in question is already removed fromthe RecyclerView.In some cases, it is acceptable to recycle a View although it has transient state. Mostof the time, this is a case where the transient state will be cleared in onBindViewHolder call when View is rebound to a new position.For this reason, RecyclerView leaves the decision to the Adapter and uses the returnvalue of this method to decide whether the View should be recycled or not.
Note that when all animations are created by ItemAnimator, youshould never receive this callback because RecyclerView keeps those Views as childrenuntil their animations are complete. This callback is useful when children of the itemviews create animations which may not be easy to implement using an ItemAnimator.
You should never fix this issue by calling
holder.itemView.setHasTransientState(false);
unless you've previously calledholder.itemView.setHasTransientState(true);
. EachView.setHasTransientState(true)
call must be matched by aView.setHasTransientState(false)
call, otherwise, the state of the Viewmay become inconsistent. You should always prefer to end or cancel animations that aretriggering the transient state instead of handling it manually.- Parameters:
holder
- The ViewHolder containing the View that could not be recycled due to itstransient state.
-
onViewAttachedToWindow
void onViewAttachedToWindow(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been attached to a window.
This can be used as a reasonable signal that the view is about to be seenby the user. If the adapter previously freed any resources in onViewDetachedFromWindow those resources should be restored here.
- Parameters:
holder
- Holder of the view being attached
-
onViewDetachedFromWindow
void onViewDetachedFromWindow(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been detached from its window.
Becoming detached from the window is not necessarily a permanent condition;the consumer of an Adapter's views may choose to cache views offscreen while theyare not visible, attaching and detaching them as appropriate.
- Parameters:
holder
- Holder of the view being detached
-
onViewRecycled
void onViewRecycled(@NonNull() RecyclerView.ViewHolder holder)
Called when a view created by this adapter has been recycled.
A view is recycled when a LayoutManager decides that it no longerneeds to be attached to its parent RecyclerView. This can be because it hasfallen out of visibility or a set of cached views represented by views stillattached to the parent RecyclerView. If an item view has large or expensive databound to it such as large bitmaps, this may be a good place to release thoseresources.
RecyclerView calls this method right before clearing ViewHolder's internal data andsending it to RecycledViewPool. This way, if ViewHolder was holding valid informationbefore being recycled, you can call getBindingAdapterPosition to getits adapter position.
- Parameters:
holder
- The ViewHolder for the view being recycled
-
onAttachedToRecyclerView
void onAttachedToRecyclerView(@NonNull() RecyclerView recyclerView)
Called by RecyclerView when it starts observing this Adapter.
Keep in mind that same adapter may be observed by multiple RecyclerViews.
- Parameters:
recyclerView
- The RecyclerView instance which started observing this adapter.
-
onDetachedFromRecyclerView
void onDetachedFromRecyclerView(@NonNull() RecyclerView recyclerView)
Called by RecyclerView when it stops observing this Adapter.
- Parameters:
recyclerView
- The RecyclerView instance which stopped observing this adapter.
-
getAdapters
@NonNull() List<out RecyclerView.Adapter<out RecyclerView.ViewHolder>> getAdapters()
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 inthis list.
-
findRelativeAdapterPositionIn
int findRelativeAdapterPositionIn(@NonNull() RecyclerView.Adapter<out RecyclerView.ViewHolder> adapter, @NonNull() RecyclerView.ViewHolder viewHolder, int globalPosition)
Returns the position of the given ViewHolder in the given Adapter.If the given Adapter is not part of this ConcatAdapter, NO_POSITION is returned.
- Parameters:
adapter
- The adapter which is a sub adapter of this ConcatAdapter or itself.viewHolder
- The view holder whose local position in the given adapter will be returned.
-
-
-
-