ScrollableState
@JvmDefaultWithCompatibility
interface ScrollableState
An object representing something that can be scrolled. This interface is implemented by states of
scrollable containers such as androidx.compose.foundation.lazy.LazyListState
or
androidx.compose.foundation.ScrollState
in order to provide low-level scrolling control via
scroll
, as well as allowing for higher-level scrolling functions like animateScrollBy
to be
implemented as extension functions on ScrollableState
.
Subclasses may also have their own methods that are specific to their interaction paradigm, such
as androidx.compose.foundation.lazy.LazyListState.scrollToItem
.
Properties
val isScrollInProgress: Boolean
Whether this ScrollableState
is currently scrolling by gesture, fling or programmatically
or not.
val canScrollForward: Boolean
Whether this ScrollableState
can scroll forward (consume a positive delta). This is
typically false if the scroll position is equal to its maximum value, and true otherwise.
Note that true
here does not imply that delta will be consumed - the ScrollableState may
decide not to handle the incoming delta (such as if it is already being scrolled separately).
Additionally, for backwards compatibility with previous versions of ScrollableState this
value defaults to true
.
val canScrollBackward: Boolean
Whether this ScrollableState
can scroll backward (consume a negative delta). This is
typically false if the scroll position is equal to its minimum value, and true otherwise.
Note that true
here does not imply that delta will be consumed - the ScrollableState may
decide not to handle the incoming delta (such as if it is already being scrolled separately).
Additionally, for backwards compatibility with previous versions of ScrollableState this
value defaults to true
.
val lastScrolledForward: Boolean
The value of this property is true under the following scenarios, otherwise it's false.
- This
ScrollableState
is currently scrolling forward. - This
ScrollableState
was scrolling forward in its last scroll action.
val lastScrolledBackward: Boolean
The value of this property is true under the following scenarios, otherwise it's false.
- This
ScrollableState
is currently scrolling backward. - This
ScrollableState
was scrolling backward in its last scroll action.
Functions
suspend fun scroll(
scrollPriority: MutatePriority = MutatePriority.Default,
block: suspend ScrollScope.() -> Unit,
)
Call this function to take control of scrolling and gain the ability to send scroll events
via ScrollScope.scrollBy
. All actions that change the logical scroll position must be
performed within a scroll
block (even if they don't call any other methods on this object)
in order to guarantee that mutual exclusion is enforced.
If scroll
is called from elsewhere with the scrollPriority
higher or equal to ongoing
scroll, ongoing scroll will be canceled.
fun dispatchRawDelta(delta: Float): Float
Dispatch scroll delta in pixels avoiding all scroll related mechanisms.
NOTE: unlike scroll
, dispatching any delta with this method won't trigger nested
scroll, won't stop ongoing scroll/drag animation and will bypass scrolling of any priority.
This method will also ignore reverseDirection
and other parameters set in scrollable.
This method is used internally for nested scrolling dispatch and other low level operations,
allowing implementers of ScrollableState
influence the consumption as suits them. Manually
dispatching delta via this method will likely result in a bad user experience, you must
prefer scroll
method over this one.
Parameters
delta | amount of scroll dispatched in the nested scroll process |
Returns
the amount of delta consumed |