Is there any way to programmatically scroll LazyColumn
to some item in the list? I thought that it can be done by hoisting the LazyColumn
argument state: LazyListState = rememberLazyListState()
but I have no idea how I can change this state e.g. on Button click.
The LazyListState
supports the scroll position via
scrollToItem()
function, which ‘immediately’ snaps the scroll position,animateScrollToItem()
which scrolls using an animationSomething like:
val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
// ...
}
Button (
onClick = {
coroutineScope.launch {
// Animate scroll to the 10th item
listState.animateScrollToItem(index = 10)
}
}
){
Text("Click")
}