androidkotlinandroid-jetpack-composeandroid-jetpack-compose-list

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


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


Solution

  • @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