Skip to content

Most visited

Recently visited

navigation

RecyclerView.State

public static class RecyclerView.State
extends Object

java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.State


Contains useful information about the current RecyclerView state like target scroll position or view focus. State object can also keep arbitrary data, identified by resource ids.

Often times, RecyclerView components will need to pass information between each other. To provide a well defined data bus between components, RecyclerView passes the same State object to component callbacks and these components can use it to exchange data.

If you implement custom components, you can use State's put/get/remove methods to pass data between your components without needing to manage their lifecycles.

Summary

Public constructors

RecyclerView.State()

Public methods

boolean didStructureChange()
<T> T get(int resourceId)

Gets the Object mapped from the specified id, or null if no such data exists.

int getItemCount()

Returns the total number of items that can be laid out.

int getRemainingScrollHorizontal()

Returns remaining horizontal scroll distance of an ongoing scroll animation(fling/ smoothScrollTo/SmoothScroller) in pixels.

int getRemainingScrollVertical()

Returns remaining vertical scroll distance of an ongoing scroll animation(fling/ smoothScrollTo/SmoothScroller) in pixels.

int getTargetScrollPosition()

If scroll is triggered to make a certain item visible, this value will return the adapter index of that item.

boolean hasTargetScrollPosition()

Returns if current scroll has a target position.

boolean isMeasuring()

Returns true if the RecyclerView is currently measuring the layout.

boolean isPreLayout()

Returns true if

void put(int resourceId, Object data)

Adds a mapping from the specified id to the specified value, replacing the previous mapping from the specified key if there was one.

void remove(int resourceId)

Removes the mapping from the specified id, if there was any.

String toString()

Returns a string representation of the object.

boolean willRunPredictiveAnimations()

Returns whether RecyclerView will run predictive animations in this layout pass or not.

boolean willRunSimpleAnimations()

Returns whether RecyclerView will run simple animations in this layout pass or not.

Inherited methods

From class java.lang.Object

Public constructors

RecyclerView.State

added in version 22.0.0
RecyclerView.State ()

Public methods

didStructureChange

added in version 22.0.0
boolean didStructureChange ()

Returns
boolean true if the structure of the data set has changed since the last call to onLayoutChildren, false otherwise

get

added in version 22.0.0
T get (int resourceId)

Gets the Object mapped from the specified id, or null if no such data exists.

Parameters
resourceId int: Id of the resource you want to remove. It is suggested to use R.id.* to preserve cross functionality and avoid conflicts.

Returns
T

getItemCount

added in version 22.0.0
int getItemCount ()

Returns the total number of items that can be laid out. Note that this number is not necessarily equal to the number of items in the adapter, so you should always use this number for your position calculations and never access the adapter directly.

RecyclerView listens for Adapter's notify events and calculates the effects of adapter data changes on existing Views. These calculations are used to decide which animations should be run.

To support predictive animations, RecyclerView may rewrite or reorder Adapter changes to present the correct state to LayoutManager in pre-layout pass.

For example, a newly added item is not included in pre-layout item count because pre-layout reflects the contents of the adapter before the item is added. Behind the scenes, RecyclerView offsets getViewForPosition(int) calls such that LayoutManager does not know about the new item's existence in pre-layout. The item will be available in second layout pass and will be included in the item count. Similar adjustments are made for moved and removed items as well.

You can get the adapter's item count via getItemCount() method.

Returns
int The number of items currently available

See also:

getRemainingScrollHorizontal

added in version 26.0.0
int getRemainingScrollHorizontal ()

Returns remaining horizontal scroll distance of an ongoing scroll animation(fling/ smoothScrollTo/SmoothScroller) in pixels. Returns zero if getScrollState() is other than SCROLL_STATE_SETTLING.

Returns
int Remaining horizontal scroll distance

getRemainingScrollVertical

added in version 26.0.0
int getRemainingScrollVertical ()

Returns remaining vertical scroll distance of an ongoing scroll animation(fling/ smoothScrollTo/SmoothScroller) in pixels. Returns zero if getScrollState() is other than SCROLL_STATE_SETTLING.

Returns
int Remaining vertical scroll distance

getTargetScrollPosition

added in version 22.0.0
int getTargetScrollPosition ()

If scroll is triggered to make a certain item visible, this value will return the adapter index of that item.

Returns
int Adapter index of the target item or NO_POSITION if there is no target position.

hasTargetScrollPosition

added in version 22.0.0
boolean hasTargetScrollPosition ()

Returns if current scroll has a target position.

Returns
boolean true if scroll is being triggered to make a certain position visible

See also:

isMeasuring

added in version 23.2.0
boolean isMeasuring ()

Returns true if the RecyclerView is currently measuring the layout. This value is true only if the LayoutManager opted into the auto measure API and RecyclerView has non-exact measurement specs.

Note that if the LayoutManager supports predictive animations and it is calculating the pre-layout step, this value will be false even if the RecyclerView is in onMeasure call. This is because pre-layout means the previous state of the RecyclerView and measurements made for that state cannot change the RecyclerView's size. LayoutManager is always guaranteed to receive another call to onLayoutChildren(Recycler, State) when this happens.

Returns
boolean True if the RecyclerView is currently calculating its bounds, false otherwise.

isPreLayout

added in version 22.0.0
boolean isPreLayout ()

Returns true if

Returns
boolean

put

added in version 22.0.0
void put (int resourceId, 
                Object data)

Adds a mapping from the specified id to the specified value, replacing the previous mapping from the specified key if there was one.

Parameters
resourceId int: Id of the resource you want to add. It is suggested to use R.id.* to preserve cross functionality and avoid conflicts.

data Object: The data you want to associate with the resourceId.

remove

added in version 22.0.0
void remove (int resourceId)

Removes the mapping from the specified id, if there was any.

Parameters
resourceId int: Id of the resource you want to remove. It is suggested to use R.id.* to preserve cross functionality and avoid conflicts.

toString

String toString ()

Returns a string representation of the object. In general, the 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())
 

Returns
String a string representation of the object.

willRunPredictiveAnimations

added in version 22.0.0
boolean willRunPredictiveAnimations ()

Returns whether RecyclerView will run predictive animations in this layout pass or not.

Returns
boolean true if RecyclerView is calculating predictive animations to be run at the end of the layout pass.

willRunSimpleAnimations

added in version 22.0.0
boolean willRunSimpleAnimations ()

Returns whether RecyclerView will run simple animations in this layout pass or not.

Returns
boolean true if RecyclerView is calculating simple animations to be run at the end of the layout pass.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)