Android APIs
public static abstract class

RecyclerView.Adapter

extends Object
java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder>
Known Direct Subclasses

Class Overview

Base class for an Adapter

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

Summary

Public Constructors
RecyclerView.Adapter()
Public Methods
final void bindViewHolder(VH holder, int position)
final VH createViewHolder(ViewGroup parent, int viewType)
abstract int getItemCount()
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 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)
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 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.
abstract void onBindViewHolder(VH holder, int position)
abstract VH onCreateViewHolder(ViewGroup parent, int viewType)
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)
void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer)
Unregister an observer currently listening for data changes.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public RecyclerView.Adapter ()

Public Methods

public final void bindViewHolder (VH holder, int position)

public final VH createViewHolder (ViewGroup parent, int viewType)

public abstract int getItemCount ()

public 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 implementation of this method returns NO_ID.

Parameters
position Adapter position to query
Returns
  • the stable ID of the item at position

public int getItemViewType (int position)

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

The default implementation of this method returns 0, making the assumption of a single view type for the adapter. Unlike ListView adapters, types need not be contiguous. Consider using id resources to uniquely identify item view types.

Parameters
position position to query
Returns
  • integer value identifying the type of the view needed to represent the item at position. Type codes need not be contiguous.

public final boolean hasObservers ()

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

Returns
  • true if this adapter has observers

public 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. If that item is relocated in the data set, the ID returned for that item should be the same.

Returns
  • true if this adapter's items have stable IDs

public final void notifyDataSetChanged ()

Notify any registered observers that the data set has changed.

There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

This event does not specify what about the data set has changed, forcing any observers to assume that all existing items and structure may no longer be valid. LayoutManagers will be forced to fully rebind and relayout all visible views.

RecyclerView will attempt to synthesize visible structural change events for adapters that report that they have stable IDs when this method is used. This can help for the purposes of animation and visual object persistence but individual item views will still need to be rebound and relaid out.

If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.

public final void notifyItemChanged (int position)

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

This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.

Parameters
position Position of the item that has changed

public final void notifyItemInserted (int position)

Notify any registered observers that the item reflected at position has been newly inserted. The item previously at position is now at position position + 1.

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

Parameters
position Position of the newly inserted item in the data set

public final void notifyItemRangeChanged (int positionStart, int itemCount)

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

This is an item change event, not a structural change event. It indicates that any reflection of the data in the given position range is out of date and should be updated. The items in the given range retain the same identity.

Parameters
positionStart Position of the first item that has changed
itemCount Number of items that have changed

public final void notifyItemRangeInserted (int positionStart, int itemCount)

Notify any registered observers that the currently reflected itemCount items starting at positionStart have been newly inserted. The items previously located at positionStart and beyond can now be found starting at position positionStart + itemCount.

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

Parameters
positionStart Position of the first item that was inserted
itemCount Number of items inserted

public 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. The items previously located at and after positionStart + itemCount may now be found at oldPosition - itemCount.

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

Parameters
positionStart Previous position of the first item that was removed
itemCount Number of items removed from the data set

public final void notifyItemRemoved (int position)

Notify any registered observers that the item previously located at position has been removed from the data set. The items previously located at and after position may now be found at oldPosition - 1.

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

Parameters
position Position of the item that has now been removed

public abstract void onBindViewHolder (VH holder, int position)

public abstract VH onCreateViewHolder (ViewGroup parent, int viewType)

public void onViewAttachedToWindow (VH 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 seen by 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

public void onViewDetachedFromWindow (VH 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 they are not visible, attaching an detaching them as appropriate.

Parameters
holder Holder of the view being detached

public void onViewRecycled (VH holder)

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

A view is recycled when a RecyclerView.LayoutManager decides that it no longer needs to be attached to its parent RecyclerView. This can be because it has fallen out of visibility or a set of cached views represented by views still attached to the parent RecyclerView. If an item view has large or expensive data bound to it such as large bitmaps, this may be a good place to release those resources.

Parameters
holder The ViewHolder for the view being recycled

public void registerAdapterDataObserver (RecyclerView.AdapterDataObserver observer)

Register a new observer to listen for data changes.

The adapter may publish a variety of events describing specific changes. Not all adapters may support all change types and some may fall back to a generic "something changed" event if more specific data is not available.

Components registering observers with an adapter are responsible for unregistering those observers when finished.

Parameters
observer Observer to register

public void setHasStableIds (boolean hasStableIds)

public void unregisterAdapterDataObserver (RecyclerView.AdapterDataObserver observer)

Unregister an observer currently listening for data changes.

The unregistered observer will no longer receive events about changes to the adapter.

Parameters
observer Observer to unregister