// 我們用這個狀態保存滾動位置
val scrollState = rememberScrollState()
Column(Modifier.verticalScroll(scrollState)) {
。。。
}
//滾動列表控件
LazyColumn {
items(100) { //注意這里不是repeat,
Text("Item #$it", style = MaterialTheme.typography.subtitle1)
}
}
val listSize = 100
// 保存滾動狀態
val scrollState = rememberLazyListState()
// 保存將執行動畫滾動的協程范圍
val coroutineScope = rememberCoroutineScope()
Column {
Row {
Button(onClick = {
//回到頂部
coroutineScope.launch {
scrollState.animateScrollToItem(0)
}
}) {
Text("Scroll to the top")
}
Button(onClick = {
//回到底部
coroutineScope.launch {
scrollState.animateScrollToItem(listSize - 1)
}
}) {
Text("Scroll to the end")
}
}
LazyColumn(state = scrollState) {
items(listSize) {
ImageListItem(it)
}
}
}