0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Kotlin]JetpackComposeでスクロール表示のリストを作る

Last updated at Posted at 2021-12-04

概要

Kotlinでは従来、XML形式でUIを作成していましたが、JetpackComposeを使えばコード上でUIを表記する事ができます。
今回はそのJetpackComposeでスクロールさせることの出来るリストを作ってみました。

JetpackComposeに慣れている方は@Composable以降を既存コードへコピペして確認してみてください。

本文

新規のKotlinファイルを作ります。テンプレートはEmptyComposeActivityを選んでください。
スクリーンショット 2021-12-04 9.17.25.png

最初に表示されるMainActivity.ktを書き換えます。

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ScrollBoxes()
        }
    }
}

@Composable
fun ScrollBoxes() {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(0.3f)
            .verticalScroll(rememberScrollState())
            .background(Color.Blue)
            .padding(15.dp,0.dp),

    ) {
        repeat(30) {
            Text(
                text = "リスト項目: $it",
                fontSize = 22.sp,
                color = Color.White,
                modifier = Modifier.clickable {
                    // 項目が押された時の処理
                }
            )
        }
    }
}

実行するとリストが表示されます。
スクリーンショット 2021-12-04 9.48.51.png

補足

デザイン用に余分な項目も加えていますが、ポイントは

modifier = Modifier.verticalScroll(rememberScrollState())

の部分だけです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?