Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

State

open class State
kotlin.Any
   ↳ androidx.recyclerview.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

Contains useful information about the current RecyclerView state like target scroll position or view focus.

Public methods

open Boolean

open T
get(resourceId: Int)

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

open Int

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

open Int

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

open Int

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

open Int

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

open Boolean

Returns if current scroll has a target position.

open Boolean

Returns true if the RecyclerView is currently measuring the layout.

open Boolean

Returns true if the RecyclerView is in the pre-layout step where it is having its LayoutManager layout items where they will be at the beginning of a set of predictive item animations.

open Unit
put(resourceId: Int, data: Any!)

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

open Unit
remove(resourceId: Int)

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

open String

open Boolean

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

open Boolean

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

Public constructors

<init>

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.

Public methods

didStructureChange

open fun didStructureChange(): Boolean
Return
Boolean: true if the structure of the data set has changed since the last call to onLayoutChildren, false otherwise

get

open fun <T : Any!> get(resourceId: Int): T

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.

getItemCount

open fun getItemCount(): Int

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 Recycler#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 LayoutManager#getItemCount() method.

Return
Int: The number of items currently available

getRemainingScrollHorizontal

open fun getRemainingScrollHorizontal(): Int

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.

Return
Int: Remaining horizontal scroll distance

getRemainingScrollVertical

open fun getRemainingScrollVertical(): Int

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.

Return
Int: Remaining vertical scroll distance

getTargetScrollPosition

open fun getTargetScrollPosition(): Int

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

Return
Int: Adapter index of the target item or RecyclerView#NO_POSITION if there is no target position.

hasTargetScrollPosition

open fun hasTargetScrollPosition(): Boolean

Returns if current scroll has a target position.

Return
Boolean: true if scroll is being triggered to make a certain position visible

isMeasuring

open fun isMeasuring(): Boolean

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 LayoutManager#onLayoutChildren(Recycler, State) when this happens.

Return
Boolean: True if the RecyclerView is currently calculating its bounds, false otherwise.

isPreLayout

open fun isPreLayout(): Boolean

Returns true if the RecyclerView is in the pre-layout step where it is having its LayoutManager layout items where they will be at the beginning of a set of predictive item animations.

put

open fun put(resourceId: Int, data: Any!): Unit

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 Int: The data you want to associate with the resourceId.

remove

open fun remove(resourceId: Int): Unit

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

open fun toString(): String

willRunPredictiveAnimations

open fun willRunPredictiveAnimations(): Boolean

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

Return
Boolean: true if RecyclerView is calculating predictive animations to be run at the end of the layout pass.

willRunSimpleAnimations

open fun willRunSimpleAnimations(): Boolean

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

Return
Boolean: true if RecyclerView is calculating simple animations to be run at the end of the layout pass.