public class TransformingLazyColumnState
@RememberInComposition
constructor(initialAnchorItemIndex: Int = -1, initialAnchorItemScrollOffset: Int = 0) :
ScrollableState
A state object that can be hoisted to control and observe scrolling in a TransformingLazyColumn.
Parameters
| initialAnchorItemIndex | The index of the item to be used as the anchor. If a non-negative index is provided, the state will attempt to center this item in the viewport. If a negative index is provided then the list will be initialized with the first item (index 0) pinned to the start of the viewport, respecting any content padding. This is the default behavior. |
| initialAnchorItemScrollOffset | The offset to be applied to the anchor item. Defaults to 0. This offset is ONLY used when a non-negative initialAnchorItemIndex is provided (i.e., when the item is being centered). It is ignored if initialAnchorItemIndex is less than 0. The offset is used when placing the item in the center of the screen; a positive value scrolls the item towards the end of the list, and a negative value scrolls it towards the start. This correlates with TransformingLazyColumnState.anchorItemScrollOffset. |
Properties
layoutInfo
public val layoutInfo: TransformingLazyColumnLayoutInfo
The object of LazyColumnLayoutInfo calculated during the last layout pass. For example, you can use it to calculate what items are currently visible. Note that this property is observable and is updated after every scroll or remeasure. If you use it in the composable function it will be recomposed on every change causing potential performance issues including infinity recomposition loop. Therefore, avoid using it in the composition. If you want to run some side effects like sending an analytics event or updating a state based on this value consider using "snapshotFlow":
anchorItemIndex
@get:FrequentlyChangingValue
public var anchorItemIndex: Int
The index of the item that is used in scrolling. For the most cases that is the item closest to the center of viewport from TransformingLazyColumnLayoutInfo.visibleItems, however it might change during scroll.
Note that this property is observable and if you use it in the composable function it will be recomposed on every change causing potential performance issues.
If you need to use it in the composition then consider wrapping the calculation into a derived state in order to only have recompositions when the derived value changes:
anchorItemScrollOffset
@get:FrequentlyChangingValue
public var anchorItemScrollOffset: Int
The scroll offset of the anchor item. Scrolling forward is positive - i.e., the amount that the item is offset backwards.
Note that this property is observable and if you use it in the composable function it will be recomposed on every scroll causing potential performance issues.
Functions
scrollToItem
public suspend fun scrollToItem(
@androidx.annotation.IntRange(from = 0) index: Int,
scrollOffset: Int = 0,
)
Scrolls the item specified by index to the center of the screen.
The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.
This operation happens instantly without animation.
Parameters
| index | The index of the item to scroll to. Must be non-negative. |
| scrollOffset | The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up. |
requestScrollToItem
public fun requestScrollToItem(
@androidx.annotation.IntRange(from = 0) index: Int,
scrollOffset: Int = 0,
)
Requests the item at index to be at the center of the viewport during the next remeasure, offset by scrollOffset.
The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.
The scroll position will be updated to the requested position rather than maintain the index based on the center item key (when a data set change will also be applied during the next remeasure), but only for the next remeasure.
Any scroll in progress will be cancelled.
Parameters
| index | the index to which to scroll. Must be non-negative. |
| scrollOffset | The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up. |
animateScrollToItem
public suspend fun animateScrollToItem(
@androidx.annotation.IntRange(from = 0) index: Int,
scrollOffset: Int = 0,
)
Animate (smooth scroll) to the given item.
The scroll position anchorItemIndex and anchorItemScrollOffset will be updated to take into account the new layout. There is no guarantee that index will become the new anchorItemIndex since requested scrollOffset may position item with another index closer to the anchor point.
Parameters
| index | the index to which to scroll. Must be non-negative. |
| scrollOffset | The offset between the center of the screen and item's center. Positive offset means the item will be scrolled up. |