RecyclerView.Adapter

public static abstract class RecyclerView.Adapter
extends Object

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<VH extends androidx.recyclerview.widget.RecyclerView.ViewHolder>


Base class for an Adapter

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView.

Summary

Nested classes

enum RecyclerView.Adapter.StateRestorationPolicy

Defines how this Adapter wants to restore its state after a view reconstruction (e.g. 

Public constructors

Adapter()

Public methods

final void bindViewHolder(VH holder, int position)

This method internally calls onBindViewHolder(ViewHolder, int) to update the RecyclerView.ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView.

final VH createViewHolder(ViewGroup parent, int viewType)

This method calls onCreateViewHolder(ViewGroup, int) to create a new RecyclerView.ViewHolder and initializes some private fields to be used by RecyclerView.

int findRelativeAdapterPositionIn(Adapter<? extends RecyclerView.ViewHolder> adapter, RecyclerView.ViewHolder viewHolder, int localPosition)

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

abstract int getItemCount()

Returns the total number of items in the data set held by the adapter.

long getItemId(int position)

Return the stable ID for the item at position.

int getItemViewType(int position)

Return the view type of the item at position for the purposes of view recycling.

final RecyclerView.Adapter.StateRestorationPolicy getStateRestorationPolicy()

Returns when this Adapter wants to restore the state.

final boolean hasObservers()

Returns true if one or more observers are attached to this adapter.

final boolean hasStableIds()

Returns true if this adapter publishes a unique long value that can act as a key for the item at a given position in the data set.

final void notifyDataSetChanged()

Notify any registered observers that the data set has changed.

final void notifyItemChanged(int position, Object payload)

Notify any registered observers that the item at position has changed with an optional payload object.

final void notifyItemChanged(int position)

Notify any registered observers that the item at position has changed.

final void notifyItemInserted(int position)

Notify any registered observers that the item reflected at position has been newly inserted.

final void notifyItemMoved(int fromPosition, int toPosition)

Notify any registered observers that the item reflected at fromPosition has been moved to toPosition.

final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload)

Notify any registered observers that the itemCount items starting at position positionStart have changed.

final void notifyItemRangeChanged(int positionStart, int itemCount)

Notify any registered observers that the itemCount items starting at position positionStart have changed.

final void notifyItemRangeInserted(int positionStart, int itemCount)

Notify any registered observers that the currently reflected itemCount items starting at positionStart have been newly inserted.

final void notifyItemRangeRemoved(int positionStart, int itemCount)

Notify any registered observers that the itemCount items previously located at positionStart have been removed from the data set.

final void notifyItemRemoved(int position)

Notify any registered observers that the item previously located at position has been removed from the data set.

void onAttachedToRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

abstract void onBindViewHolder(VH holder, int position)

Called by RecyclerView to display the data at the specified position.

void onBindViewHolder(VH holder, int position, List<Object> payloads)

Called by RecyclerView to display the data at the specified position.

abstract VH onCreateViewHolder(ViewGroup parent, int viewType)

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

void onDetachedFromRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it stops observing this Adapter.

boolean onFailedToRecycleView(VH holder)

Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state.

void onViewAttachedToWindow(VH holder)

Called when a view created by this adapter has been attached to a window.

void onViewDetachedFromWindow(VH holder)

Called when a view created by this adapter has been detached from its window.

void onViewRecycled(VH holder)

Called when a view created by this adapter has been recycled.

void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer)

Register a new observer to listen for data changes.

void setHasStableIds(boolean hasStableIds)

Indicates whether each item in the data set can be represented with a unique identifier of type Long.

void setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy strategy)

Sets the state restoration strategy for the Adapter.

void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer)

Unregister an observer currently listening for data changes.

Inherited methods

Public constructors

Adapter

public Adapter ()

Public methods

bindViewHolder

public final void bindViewHolder (VH holder, 
                int position)

This method internally calls onBindViewHolder(ViewHolder, int) to update the RecyclerView.ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView. Adapters that merge other adapters should use bindViewHolder(ViewHolder, int) when calling nested adapters so that RecyclerView can track which adapter bound the RecyclerView.ViewHolder to return the correct position from RecyclerView.ViewHolder.getBindingAdapterPosition() method. They should also override the findRelativeAdapterPositionIn(Adapter, ViewHolder, int) method.

Parameters
holder VH: The view holder whose contents should be updated

position int: The position of the holder with respect to this adapter

createViewHolder

public final VH createViewHolder (ViewGroup parent, 
                int viewType)

This method calls onCreateViewHolder(ViewGroup, int) to create a new RecyclerView.ViewHolder and initializes some private fields to be used by RecyclerView.

Parameters
parent ViewGroup

viewType int

Returns
VH

findRelativeAdapterPositionIn

public int findRelativeAdapterPositionIn (Adapter<? extends RecyclerView.ViewHolder> adapter, 
                RecyclerView.ViewHolder viewHolder, 
                int localPosition)

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

Parameters
adapter Adapter: The adapter which is a sub adapter of this adapter or itself.

viewHolder RecyclerView.ViewHolder: The ViewHolder whose local position in the given adapter will be returned.

localPosition int: The position of the given RecyclerView.ViewHolder in this RecyclerView.Adapter.

Returns
int The local position of the given RecyclerView.ViewHolder in this RecyclerView.Adapter or RecyclerView.NO_POSITION if the RecyclerView.ViewHolder is not bound to an item or the given RecyclerView.Adapter is not part of this Adapter (if this Adapter merges other adapters).