Jetpack Compose LazyColumn 以编程方式滚动到 Item

ikn*_*now 15 android kotlin android-jetpack-compose android-jetpack-compose-list

有没有办法以编程方式滚动LazyColumn到列表中的某个项目?我认为可以通过提升LazyColumn参数来完成,state: LazyListState = rememberLazyListState()但我不知道如何更改此状态,例如单击按钮。

Gab*_*tti 12

通过1.0.x LazyListState支持滚动位置

就像是:

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

  
Run Code Online (Sandbox Code Playgroud)