Reputation: 571
How can I make the lazyRow go to the last index when I launch my app?
Upvotes: 0
Views: 1791
Reputation: 364451
You can use the method animateScrollToItem
:
Something like:
val itemsList = //... your list
val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
items(itemsList){
Text( "Item $it" )
}
}
To launch automatically you can use:
DisposableEffect(Unit) {
coroutineScope.launch {
listState.animateScrollToItem(index = itemsList.size-1)
}
onDispose { }
}
Upvotes: 1
Reputation: 812
@Composable
fun MessageList(messages: List<Message>) {
val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
// ...
}
ScrollToTopButton(
onClick = {
coroutineScope.launch {
// Animate scroll to the first item
listState.animateScrollToItem(index = lastIndex)
}
}
)
}
Please refer the docs for more here
You can do the same thing for LazyRow
Upvotes: 2