How do I start a lazyRow in the last index on Jetpack Compose?

1.5k views Asked by At

How can I make the lazyRow go to the last index when I launch my app?

2

There are 2 answers

2
End User On BEST ANSWER
@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

0
Gabriele Mariotti On

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 { }
}