0

启动应用程序时,如何使lazyRow 转到最后一个索引?

4

2 回答 2

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

请在此处参考文档以获取更多信息

你可以为 LazyRow 做同样的事情

于 2021-04-16T12:26:30.560 回答
0

您可以使用以下方法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 { }
}
于 2021-04-16T12:58:04.593 回答