T - The type of the datasource of the adapterpublic class AdapterDelegatesManager<T>
extends java.lang.Object
RecyclerView.Adapter together with AdapterDelegate.
So you have to add / register your AdapterDelegates to this manager by calling addDelegate(AdapterDelegate)
Next you have to add this AdapterDelegatesManager to the RecyclerView.Adapter by calling
corresponding methods:
getItemViewType(Object, int): Must be called from RecyclerView.Adapter#getItemViewType(int)onCreateViewHolder(ViewGroup, int): Must be called from RecyclerView.Adapter#onCreateViewHolder(ViewGroup, int)onBindViewHolder(Object, int, RecyclerView.ViewHolder): Must be called from RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int)
You can also set a fallback AdapterDelegate by using setFallbackDelegate(AdapterDelegate) that will be used if no AdapterDelegate is
responsible to handle a certain view type. If no fallback is specified, an Exception will be
thrown if no AdapterDelegate is responsible to handle a certain view type
| Modifier and Type | Field and Description |
|---|---|
protected androidx.collection.SparseArrayCompat<AdapterDelegate<T>> |
delegates
Map for ViewType to AdapterDelegate
|
static int |
FALLBACK_DELEGATE_VIEW_TYPE
ViewType for the fallback delegate
|
protected AdapterDelegate<T> |
fallbackDelegate |
| Constructor and Description |
|---|
AdapterDelegatesManager() |
| Modifier and Type | Method and Description |
|---|---|
AdapterDelegatesManager<T> |
addDelegate(AdapterDelegate<T> delegate)
Adds an
AdapterDelegate. |
AdapterDelegatesManager<T> |
addDelegate(int viewType,
AdapterDelegate<T> delegate)
Adds an
AdapterDelegate with the specified view type. |
AdapterDelegatesManager<T> |
addDelegate(int viewType,
boolean allowReplacingDelegate,
AdapterDelegate<T> delegate)
Adds an
AdapterDelegate. |
AdapterDelegate<T> |
getDelegateForViewType(int viewType)
Get the
AdapterDelegate associated with the given view type integer |
AdapterDelegate<T> |
getFallbackDelegate()
Get the fallback delegate
|
int |
getItemViewType(T items,
int position)
Must be called from
RecyclerView.Adapter#getItemViewType(int). |
int |
getViewType(AdapterDelegate<T> delegate)
Get the view type integer for the given
AdapterDelegate |
void |
onBindViewHolder(T items,
int position,
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
Must be called from
RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int,
List) |
void |
onBindViewHolder(T items,
int position,
androidx.recyclerview.widget.RecyclerView.ViewHolder holder,
java.util.List payloads)
Must be called from
RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int,
List) |
androidx.recyclerview.widget.RecyclerView.ViewHolder |
onCreateViewHolder(android.view.ViewGroup parent,
int viewType)
This method must be called in
RecyclerView.Adapter#onCreateViewHolder(ViewGroup, int) |
boolean |
onFailedToRecycleView(androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
Must be called from
RecyclerView.Adapter#onFailedToRecycleView(RecyclerView.ViewHolder) |
void |
onViewAttachedToWindow(androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
Must be called from
RecyclerView.Adapter#onViewAttachedToWindow(RecyclerView.ViewHolder) |
void |
onViewDetachedFromWindow(androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
Must be called from
RecyclerView.Adapter#onViewDetachedFromWindow(RecyclerView.ViewHolder) |
void |
onViewRecycled(androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
Must be called from
RecyclerView.Adapter#onViewRecycled(RecyclerView.ViewHolder) |
AdapterDelegatesManager<T> |
removeDelegate(AdapterDelegate<T> delegate)
Removes a previously registered delegate if and only if the passed delegate is registered
(checks the reference of the object).
|
AdapterDelegatesManager<T> |
removeDelegate(int viewType)
Removes the adapterDelegate for the given view types.
|
AdapterDelegatesManager<T> |
setFallbackDelegate(AdapterDelegate<T> fallbackDelegate)
Set a fallback delegate that should be used if no
AdapterDelegate has been found that
can handle a certain view type. |
public static final int FALLBACK_DELEGATE_VIEW_TYPE
protected androidx.collection.SparseArrayCompat<AdapterDelegate<T>> delegates
protected AdapterDelegate<T> fallbackDelegate
public AdapterDelegatesManager<T> addDelegate(@NonNull AdapterDelegate<T> delegate)
AdapterDelegate.
This method automatically assign internally the view type integer by using the next
unused
Internally calls addDelegate(int, boolean, AdapterDelegate) with
allowReplacingDelegate = false as parameter.
delegate - the delegate to addjava.lang.NullPointerException - if passed delegate is nulladdDelegate(int, AdapterDelegate),
addDelegate(int, boolean, AdapterDelegate)public AdapterDelegatesManager<T> addDelegate(int viewType, @NonNull AdapterDelegate<T> delegate)
AdapterDelegate with the specified view type.
Internally calls addDelegate(int, boolean, AdapterDelegate) with
allowReplacingDelegate = false as parameter.
viewType - the view type integer if you want to assign manually the view type. Otherwise
use addDelegate(AdapterDelegate) where a viewtype will be assigned automatically.delegate - the delegate to addjava.lang.NullPointerException - if passed delegate is nulladdDelegate(AdapterDelegate),
addDelegate(int, boolean, AdapterDelegate)public AdapterDelegatesManager<T> addDelegate(int viewType, boolean allowReplacingDelegate, @NonNull AdapterDelegate<T> delegate)
AdapterDelegate.viewType - The viewType idallowReplacingDelegate - if true, you allow to replacing the given delegate any previous
delegate for the same view type. if false, you disallow and a IllegalArgumentException
will be thrown if you try to replace an already registered AdapterDelegate for the
same view type.delegate - The delegate to addjava.lang.IllegalArgumentException - if allowReplacingDelegate is false and an AdapterDelegate is already added (registered)
with the same ViewType.java.lang.IllegalArgumentException - if viewType is FALLBACK_DELEGATE_VIEW_TYPE which is
reservedaddDelegate(AdapterDelegate),
addDelegate(int, AdapterDelegate),
setFallbackDelegate(AdapterDelegate)public AdapterDelegatesManager<T> removeDelegate(@NonNull AdapterDelegate<T> delegate)
delegate - The delegate to removepublic AdapterDelegatesManager<T> removeDelegate(int viewType)
viewType - The Viewtypepublic int getItemViewType(@NonNull
T items,
int position)
RecyclerView.Adapter#getItemViewType(int). Internally it scans all
the registered AdapterDelegate and picks the right one to return the ViewType integer.items - Adapter's data sourceposition - the position in adapters data sourceFALLBACK_DELEGATE_VIEW_TYPE in case that the
fallback adapter delegate should be usedjava.lang.NullPointerException - if no AdapterDelegate has been found that is
responsible for the given data element in data set (No AdapterDelegate for the given
ViewType)java.lang.NullPointerException - if items is null@NonNull
public androidx.recyclerview.widget.RecyclerView.ViewHolder onCreateViewHolder(@NonNull
android.view.ViewGroup parent,
int viewType)
RecyclerView.Adapter#onCreateViewHolder(ViewGroup, int)parent - the parentviewType - the view typejava.lang.NullPointerException - if no AdapterDelegate has been registered for ViewHolders
viewTypepublic void onBindViewHolder(@NonNull
T items,
int position,
@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder,
java.util.List payloads)
RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int,
List)items - Adapter's data sourceposition - the position in data sourceholder - the ViewHolder to bindpayloads - A non-null list of merged payloads. Can be empty list if requires full update.java.lang.NullPointerException - if no AdapterDelegate has been registered for ViewHolders
viewTypepublic void onBindViewHolder(@NonNull
T items,
int position,
@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int,
List)items - Adapter's data sourceposition - the position in data sourceholder - the ViewHolder to bindjava.lang.NullPointerException - if no AdapterDelegate has been registered for ViewHolders
viewTypepublic void onViewRecycled(@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
RecyclerView.Adapter#onViewRecycled(RecyclerView.ViewHolder)holder - The ViewHolder for the view being recycledpublic boolean onFailedToRecycleView(@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
RecyclerView.Adapter#onFailedToRecycleView(RecyclerView.ViewHolder)holder - The ViewHolder containing the View that could not be recycled due to its
transient state.true, RecyclerView will ignore the transient state of
the View and recycle it regardless. If this method returns false,
RecyclerView will check the View's transient state again before giving a final decision.
Default implementation returns false.public void onViewAttachedToWindow(@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
RecyclerView.Adapter#onViewAttachedToWindow(RecyclerView.ViewHolder)holder - Holder of the view being attachedpublic void onViewDetachedFromWindow(@NonNull
androidx.recyclerview.widget.RecyclerView.ViewHolder holder)
RecyclerView.Adapter#onViewDetachedFromWindow(RecyclerView.ViewHolder)holder - Holder of the view being attachedpublic AdapterDelegatesManager<T> setFallbackDelegate(@Nullable AdapterDelegate<T> fallbackDelegate)
AdapterDelegate has been found that
can handle a certain view type.fallbackDelegate - The AdapterDelegate that should be used as fallback if no
other AdapterDelegate has handled a certain view type. null you can set this to
null if
you want to remove a previously set fallback AdapterDelegatepublic int getViewType(@NonNull
AdapterDelegate<T> delegate)
AdapterDelegatedelegate - The delegate we want to know the view type for@Nullable public AdapterDelegate<T> getDelegateForViewType(int viewType)
AdapterDelegate associated with the given view type integerviewType - The view type integer we want to retrieve the associated
delegate for.AdapterDelegate associated with the view type param if it exists,
the fallback delegate otherwise if it is set or returns null if no delegate is
associated to this viewType (and no fallback has been set).@Nullable public AdapterDelegate<T> getFallbackDelegate()
null if no fallback delegate has been setsetFallbackDelegate(AdapterDelegate)