LoginSignup
0
0

JetpackCompose LazyColumnを使用して、指定したindexにスクロールする方法

Posted at

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

Screenshot_20240519_182719.png

実行した結果10件目のアイテムが一番上に表示されてます。

listState.scrollToItem(index = 10, scrollOffset = 0)
こちらのコードの引数でindexにわたしたアイテムが一番上に表示されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0