简体   繁体   English

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

[英]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?启动应用程序时,如何将lazyRow go 设置为最后一个索引?

@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你可以为 LazyRow 做同样的事情

You can use the method animateScrollToItem :您可以使用方法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 { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM