Is there any way to programmatically scroll LazyColumn to some item in the list? I thought that it can be done by hoisting the LazyColumn argument state: LazyListState = rememberLazyListState() but I have no idea how I can change this state e.g. on Button click.

推荐答案

With 1.0.x LazyListState supports the scroll position via

比如:

val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()

LazyColumn(state = listState) {
    // ...
}

Button (
    onClick = { 
        coroutineScope.launch {
            // Animate scroll to the 10th item
            listState.animateScrollToItem(index = 10)
        }
    }
){
    Text("Click")
}

  

Kotlin相关问答推荐

访问者闭包中的Kotlin序列yield 率

Kotlin 协程按顺序执行,但仅在生产机器上执行

如何避免键盘打开时jetpack compose 内容上升

在 Kotlin 协程中切换 IO 和 UI 的正确方法是什么?

将 SharedPreferences 中的值公开为流

如何将 `throw` 放置在辅助函数中但仍然具有空安全性?

使用 Compose for Desktop Bundle 文件

Kotlin - 当表达式返回函数类型

Kotlin 中的 Java Scanner 相当于什么?

IntelliJ 不会根据 ktlint 的期望对 Kotlin 导入进行排序

是否可以在 kotlin 中嵌套数据类?

Kotlin - mutableMapOf() 会保留我输入的顺序

Kotlin Compose,在行中对齐元素

Hilt Activity 必须附加到 @AndroidEntryPoint 应用程序

有没有办法在数据类构建时转换属性的值?

Kotlin val difference getter override vs assignment

TypeConverter()在Android的TypeConverter错误中具有私有访问权限

如何序列化/反序列化Kotlin密封类?

在 Kotlin 中返回函数有不那么丑陋的方法吗?

可见性不适用于 Kotlin