With 1.0.0-beta06
the LazyColumn
produces a vertically scrolling list.
Something like:
val itemsList = (0..30).toList()
LazyColumn {
items(itemsList) {
Text("Item is $it")
}
}
The LazyListState
is a state object that can be hoisted to control and observe scrolling. It is created via rememberLazyListState
.
val listState = rememberLazyListState()
It can be used to react and listen to scroll position and item layout changes.
// Provide it to LazyColumn
LazyColumn(state = liststate) {
// Check if the first visible item is past the first item
if (listState.firstVisibleItemIndex > 0){
//...
}
}
or to control the scroll position :
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
// ...
}
lazyListState.animateScrollToItem(lazyListState.firstVisibleItemIndex)
Button (
onClick = {
coroutineScope.launch {
// Animate scroll to item with index=5
listState.animateScrollToItem(index = 5)
}
}
){
Text("Click")
}
rememberLazyListState
too? Is it for saving and restoring scroll position? – Orianna