如何在 Jetpack Compose 的最后一个索引中启动 lazyRow?

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

如何在启动我的应用程序时让 lazyRow 转到最后一个索引?

@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)
            }
        }
    )
}

更多内容请参考文档here

您可以对 LazyRow 做同样的事情

您可以使用方法 animateScrollToItem:

类似于:

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" )
    }
}

要自动启动,您可以使用:

DisposableEffect(Unit) {
    coroutineScope.launch {
        listState.animateScrollToItem(index = itemsList.size-1)
    }
    onDispose { }
}