RecyclerView.ViewHolder

This package is part of the
Android support library which
is no longer maintained.
The support library has been superseded by AndroidX
which is part of Jetpack.
We recommend using the AndroidX libraries in all new projects. You should also consider
migrating existing projects to AndroidX.

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

This method was deprecated
in API level 22.1.0.
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.

Public constructors

RecyclerView.ViewHolder

Public methods

getAdapterPosition

Returns the Adapter position of the item represented by this ViewHolder.

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.

The adapter position of the item if it still exists in the adapter.
NO_POSITION if item has been removed from the adapter,
notifyDataSetChanged() has been called after the last
layout pass or the ViewHolder has already been recycled.

getItemViewType

getLayoutPosition

Returns the position of the ViewHolder in terms of the latest layout pass.

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.

getPosition

This method was deprecated
in API level 22.1.0.
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.

setIsRecyclable

Informs the recycler whether this item can be recycled. Views which are not
recyclable will not be reused for other items until setIsRecyclable() is
later set to true. Calls to setIsRecyclable() should always be paired (one
call to setIsRecyclabe(false) should always be matched with a later call to
setIsRecyclable(true)). Pairs of calls may be nested, as the state is internally
reference-counted.

Parameters

recyclable

boolean: Whether this item is available to be recycled. Default value
is true.