public abstract static class RecyclerView.ViewHolder extends Object
RecyclerView.Adapter
implementations should subclass ViewHolder and add fields for caching
potentially expensive View.findViewById(int)
results.
While RecyclerView.LayoutParams
belong to the RecyclerView.LayoutManager
,
ViewHolders
belong to the adapter. Adapters should feel free to use
their own custom ViewHolder implementations to store data that makes binding view contents
easier. Implementations should assume that individual item views will hold strong references
to ViewHolder
objects and that RecyclerView
instances may hold
strong references to extra off-screen item views for caching purposes
Constructor and Description |
---|
ViewHolder(View itemView) |
Modifier and Type | Method and Description |
---|---|
int |
getAdapterPosition()
Returns the Adapter position of the item represented by this ViewHolder.
|
long |
getItemId()
Returns The itemId represented by this ViewHolder.
|
int |
getItemViewType() |
int |
getLayoutPosition()
Returns the position of the ViewHolder in terms of the latest layout pass.
|
int |
getOldPosition()
When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders
to perform animations.
|
int |
getPosition()
Deprecated.
This method is deprecated because its meaning is ambiguous due to the async
handling of adapter updates. Please use
getLayoutPosition() or
getAdapterPosition() depending on your use case. |
boolean |
isRecyclable() |
void |
setIsRecyclable(boolean recyclable)
Informs the recycler whether this item can be recycled.
|
String |
toString()
Returns a string representation of the object.
|
public final View itemView
public ViewHolder(View itemView)
@Deprecated public final int getPosition()
getLayoutPosition()
or
getAdapterPosition()
depending on your use case.getLayoutPosition()
,
getAdapterPosition()
public final int getLayoutPosition()
This position is mostly used by RecyclerView components to be consistent while RecyclerView lazily processes adapter updates.
For performance and animation reasons, RecyclerView batches all adapter updates until the next layout pass. This may cause mismatches between the Adapter position of the item and the position it had in the latest layout calculations.
LayoutManagers should always call this method while doing calculations based on item
positions. All methods in RecyclerView.LayoutManager
, RecyclerView.State
,
RecyclerView.Recycler
that receive a position expect it to be the layout position
of the item.
If LayoutManager needs to call an external method that requires the adapter position of
the item, it can use getAdapterPosition()
or
RecyclerView.Recycler.convertPreLayoutPositionToPostLayout(int)
.
getAdapterPosition()
public final int getAdapterPosition()
Note that this might be different than the getLayoutPosition()
if there are
pending adapter updates but a new layout pass has not happened yet.
RecyclerView does not handle any adapter updates until the next layout traversal. This may create temporary inconsistencies between what user sees on the screen and what adapter contents have. This inconsistency is not important since it will be less than 16ms but it might be a problem if you want to use ViewHolder position to access the adapter. Sometimes, you may need to get the exact adapter position to do some actions in response to user events. In that case, you should use this method which will calculate the Adapter position of the ViewHolder.
Note that if you've called RecyclerView.Adapter.notifyDataSetChanged()
, until the
next layout pass, the return value of this method will be RecyclerView.NO_POSITION
.
RecyclerView.NO_POSITION
if item has been removed from the adapter,
RecyclerView.Adapter.notifyDataSetChanged()
has been called after the last
layout pass or the ViewHolder has already been recycled.public final int getOldPosition()
If a ViewHolder was laid out in the previous onLayout call, old position will keep its adapter index in the previous layout.
RecyclerView.NO_POSITION
if old position does not exists or cleared (pre-layout is
complete).public final long getItemId()
RecyclerView.NO_ID
otherwisepublic final int getItemViewType()
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public final void setIsRecyclable(boolean recyclable)
recyclable
- Whether this item is available to be recycled. Default value
is true.public final boolean isRecyclable()
#setIsRecyclable(boolean)}