LazyColumnを使用して、指定したindexにスクロールする方法です。
今後も使うことがありそうなので、こちらに実装方法を記載します。
サンプルコード
Sample.kt
@Composable
fun Sample() {
val sampleList = mutableListOf<String>()
repeat(100) {
sampleList.add("アイテム$it")
}
Column(
modifier = Modifier
.fillMaxSize()
) {
val listState = rememberLazyListState()
val scope = rememberCoroutineScope()
LazyColumn(
state = listState
) {
items(sampleList) {
Text(
text = it,
modifier = Modifier.height(30.dp),
)
}
scope.launch {
listState.scrollToItem(index = 10, scrollOffset = 0)
}
}
}
}
実行した結果10件目
のアイテムが一番上に表示されてます。
listState.scrollToItem(index = 10, scrollOffset = 0)
こちらのコードの引数でindex
にわたしたアイテムが一番上に表示されます。