public abstract static class RecyclerView.Adapter<VH extends RecyclerView.ViewHolder> extends Object
Adapters provide a binding from an app-specific data set to views that are displayed
within a RecyclerView
.
Constructor and Description |
---|
Adapter() |
Modifier and Type | Method and Description |
---|---|
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. |
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. |
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. |
boolean |
hasObservers()
Returns true if one or more observers are attached to this adapter.
|
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. |
void |
notifyDataSetChanged()
Notify any registered observers that the data set has changed.
|
void |
notifyItemChanged(int position)
Notify any registered observers that the item at
position has changed. |
void |
notifyItemChanged(int position,
Object payload)
Notify any registered observers that the item at
position has changed with an
optional payload object. |
void |
notifyItemInserted(int position)
Notify any registered observers that the item reflected at
position
has been newly inserted. |
void |
notifyItemMoved(int fromPosition,
int toPosition)
Notify any registered observers that the item reflected at
fromPosition
has been moved to toPosition . |
void |
notifyItemRangeChanged(int positionStart,
int itemCount)
Notify any registered observers that the
itemCount items starting at
position positionStart have changed. |
void |
notifyItemRangeChanged(int positionStart,
int itemCount,
Object payload)
Notify any registered observers that the
itemCount items starting at
position positionStart have changed. |
void |
notifyItemRangeInserted(int positionStart,
int itemCount)
Notify any registered observers that the currently reflected
itemCount
items starting at positionStart have been newly inserted. |
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. |
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 |
unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer)
Unregister an observer currently listening for data changes.
|
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType)
RecyclerView.ViewHolder
of the given type to represent
an item.
This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.
The new ViewHolder will be used to display items of the adapter using
#onBindViewHolder(ViewHolder, int, List)
. Since it will be re-used to display
different items in the data set, it is a good idea to cache references to sub views of
the View to avoid unnecessary View.findViewById(int)
calls.
parent
- The ViewGroup into which the new View will be added after it is bound to
an adapter position.viewType
- The view type of the new View.getItemViewType(int)
,
#onBindViewHolder(ViewHolder, int)
public abstract void onBindViewHolder(VH holder, int position)
RecyclerView.ViewHolder.itemView
to reflect the item at the given
position.
Note that unlike ListView
, RecyclerView will not call this method
again if the position of the item changes in the data set unless the item itself is
invalidated or the new position cannot be determined. For this reason, you should only
use the position
parameter while acquiring the related data item inside
this method and should not keep a copy of it. If you need the position of an item later
on (e.g. in a click listener), use RecyclerView.ViewHolder.getAdapterPosition()
which will
have the updated adapter position.
Override #onBindViewHolder(ViewHolder, int, List)
instead if Adapter can
handle efficient partial bind.
holder
- The ViewHolder which should be updated to represent the contents of the
item at the given position in the data set.position
- The position of the item within the adapter's data set.public void onBindViewHolder(VH holder, int position, List<Object> payloads)
RecyclerView.ViewHolder.itemView
to reflect the item at
the given position.
Note that unlike ListView
, RecyclerView will not call this method
again if the position of the item changes in the data set unless the item itself is
invalidated or the new position cannot be determined. For this reason, you should only
use the position
parameter while acquiring the related data item inside
this method and should not keep a copy of it. If you need the position of an item later
on (e.g. in a click listener), use RecyclerView.ViewHolder.getAdapterPosition()
which will
have the updated adapter position.
Partial bind vs full bind:
The payloads parameter is a merge list from notifyItemChanged(int, Object)
or
notifyItemRangeChanged(int, int, Object)
. If the payloads list is not empty,
the ViewHolder is currently bound to old data and Adapter may run an efficient partial
update using the payload info. If the payload is empty, Adapter must run a full bind.
Adapter should not assume that the payload passed in notify methods will be received by
onBindViewHolder(). For example when the view is not attached to the screen, the
payload in notifyItemChange() will be simply dropped.
holder
- The ViewHolder which should be updated to represent the contents of the
item at the given position in the data set.position
- The position of the item within the adapter's data set.payloads
- A non-null list of merged payloads. Can be empty list if requires full
update.public final VH createViewHolder(ViewGroup parent, int viewType)
onCreateViewHolder(ViewGroup, int)
to create a new
RecyclerView.ViewHolder
and initializes some private fields to be used by RecyclerView.onCreateViewHolder(ViewGroup, int)
public final void bindViewHolder(VH holder, int position)
#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.#onBindViewHolder(ViewHolder, int)
public int getItemViewType(int position)
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.
position
- position to queryposition
. Type codes need not be contiguous.public void setHasStableIds(boolean hasStableIds)
Long
.hasStableIds
- Whether items in data set have unique identifiers or not.hasStableIds()
,
getItemId(int)
public long getItemId(int position)
position
. If hasStableIds()
would return false this method should return RecyclerView.NO_ID
. The default implementation
of this method returns RecyclerView.NO_ID
.position
- Adapter position to querypublic abstract int getItemCount()
public final boolean hasStableIds()
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.public void onViewRecycled(VH holder)
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.
RecyclerView calls this method right before clearing ViewHolder's internal data and
sending it to RecycledViewPool. This way, if ViewHolder was holding valid information
before being recycled, you can call RecyclerView.ViewHolder.getAdapterPosition()
to get
its adapter position.
holder
- The ViewHolder for the view being recycledpublic boolean onFailedToRecycleView(VH holder)
true
so that
the View can be recycled. Keep in mind that the View in question is already removed from
the RecyclerView.
In some cases, it is acceptable to recycle a View although it has transient state. Most
of the time, this is a case where the transient state will be cleared in
#onBindViewHolder(ViewHolder, int)
call when View is rebound to a new position.
For this reason, RecyclerView leaves the decision to the Adapter and uses the return
value of this method to decide whether the View should be recycled or not.
Note that when all animations are created by RecyclerView.ItemAnimator
, you
should never receive this callback because RecyclerView keeps those Views as children
until their animations are complete. This callback is useful when children of the item
views create animations which may not be easy to implement using an RecyclerView.ItemAnimator
.
You should never fix this issue by calling
holder.itemView.setHasTransientState(false);
unless you've previously called
holder.itemView.setHasTransientState(true);
. Each
View.setHasTransientState(true)
call must be matched by a
View.setHasTransientState(false)
call, otherwise, the state of the View
may become inconsistent. You should always prefer to end or cancel animations that are
triggering the transient state instead of handling it manually.
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(VH holder)
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.
holder
- Holder of the view being attachedpublic void onViewDetachedFromWindow(VH holder)
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 and detaching them as appropriate.
holder
- Holder of the view being detachedpublic final boolean hasObservers()
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer)
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.
observer
- Observer to registerunregisterAdapterDataObserver(RecyclerView.AdapterDataObserver)
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer)
The unregistered observer will no longer receive events about changes to the adapter.
observer
- Observer to unregisterregisterAdapterDataObserver(RecyclerView.AdapterDataObserver)
public void onAttachedToRecyclerView(RecyclerView recyclerView)
Keep in mind that same adapter may be observed by multiple RecyclerViews.
recyclerView
- The RecyclerView instance which started observing this adapter.onDetachedFromRecyclerView(RecyclerView)
public void onDetachedFromRecyclerView(RecyclerView recyclerView)
recyclerView
- The RecyclerView instance which stopped observing this adapter.onAttachedToRecyclerView(RecyclerView)
public final void notifyDataSetChanged()
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)
position
has changed.
Equivalent to calling notifyItemChanged(position, null);
.
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.
position
- Position of the item that has changednotifyItemRangeChanged(int, int)
public final void notifyItemChanged(int position, Object payload)
position
has changed with an
optional payload object.
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.
Client can optionally pass a payload for partial change. These payloads will be merged
and may be passed to adapter's #onBindViewHolder(ViewHolder, int, List)
if the
item is already represented by a ViewHolder and it will be rebound to the same
ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing
payloads on that item and prevent future payload until
#onBindViewHolder(ViewHolder, int, List)
is called. Adapter should not assume
that the payload will always be passed to onBindViewHolder(), e.g. when the view is not
attached, the payload will be simply dropped.
position
- Position of the item that has changedpayload
- Optional parameter, use null to identify a "full" updatenotifyItemRangeChanged(int, int)
public final void notifyItemRangeChanged(int positionStart, int itemCount)
itemCount
items starting at
position positionStart
have changed.
Equivalent to calling notifyItemRangeChanged(position, itemCount, null);
.
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.
positionStart
- Position of the first item that has changeditemCount
- Number of items that have changednotifyItemChanged(int)
public final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload)
itemCount
items starting at
position positionStart
have changed. An optional payload can be
passed to each changed item.
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.
Client can optionally pass a payload for partial change. These payloads will be merged
and may be passed to adapter's #onBindViewHolder(ViewHolder, int, List)
if the
item is already represented by a ViewHolder and it will be rebound to the same
ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing
payloads on that item and prevent future payload until
#onBindViewHolder(ViewHolder, int, List)
is called. Adapter should not assume
that the payload will always be passed to onBindViewHolder(), e.g. when the view is not
attached, the payload will be simply dropped.
positionStart
- Position of the first item that has changeditemCount
- Number of items that have changedpayload
- Optional parameter, use null to identify a "full" updatenotifyItemChanged(int)
public final void notifyItemInserted(int position)
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.
position
- Position of the newly inserted item in the data setnotifyItemRangeInserted(int, int)
public final void notifyItemMoved(int fromPosition, int toPosition)
fromPosition
has been moved to toPosition
.
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.
fromPosition
- Previous position of the item.toPosition
- New position of the item.public final void notifyItemRangeInserted(int positionStart, int itemCount)
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.
positionStart
- Position of the first item that was inserteditemCount
- Number of items insertednotifyItemInserted(int)
public final void notifyItemRemoved(int position)
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.
position
- Position of the item that has now been removednotifyItemRangeRemoved(int, int)
public final void notifyItemRangeRemoved(int positionStart, int itemCount)
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.
positionStart
- Previous position of the first item that was removeditemCount
- Number of items removed from the data set