Android APIs
public static abstract class

RecyclerView.ViewHolder

extends Object
java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.ViewHolder
Known Direct Subclasses

Class Overview

A ViewHolder describes an item view and metadata about its place within the RecyclerView.

RecyclerView.Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive 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

Summary

Fields
public final View itemView
Public Constructors
RecyclerView.ViewHolder(View itemView)
Public Methods
final long getItemId()
Returns The itemId represented by this ViewHolder.
final int getItemViewType()
final int getOldPosition()
When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders to perform animations.
final int getPosition()
final boolean isRecyclable()
final void setIsRecyclable(boolean recyclable)
Informs the recycler whether this item can be recycled.
String toString()
Returns a string containing a concise, human-readable description of this object.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public final View itemView

Public Constructors

public RecyclerView.ViewHolder (View itemView)

Public Methods

public final long getItemId ()

Returns The itemId represented by this ViewHolder.

Returns
  • The the item's id if adapter has stable ids, NO_ID otherwise

public final int getItemViewType ()

Returns
  • The view type of this ViewHolder.

public final int getOldPosition ()

When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders to perform animations.

If a ViewHolder was laid out in the previous onLayout call, old position will keep its adapter index in the previous layout.

Returns
  • The previous adapter index of the Item represented by this ViewHolder or NO_POSITION if old position does not exists or cleared (pre-layout is complete).

public final int getPosition ()

public final boolean isRecyclable ()

Returns
  • true if this item is available to be recycled, false otherwise.

public final void setIsRecyclable (boolean recyclable)

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 Whether this item is available to be recycled. Default value is true.

public String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.