2
1

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 1 year has passed since last update.

JetpackComposeのHorizontalPagerを使ってみた。

Posted at

今回はHorizontalPagerを使用してみました。

今回は下記1.4.0での実装方法です。

"androidx.compose.foundation:foundation:1.4.0"

サンプルコード

HorizontalPagerSample.kt
const val PAGE = 3
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HorizontalPagerSample() {
    val pagerState = rememberPagerState(initialPage = 0)
    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        HorizontalPager(
            state = pagerState,
            pageCount = PAGE
        ) { page ->
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .width(300.dp)
                    .height(200.dp)
                    .background(
                        color = Color.Gray,
                        shape = RoundedCornerShape(8.dp)
                    )
            ) {
                Text(text = "ページ数: $page")
            }
        }
    }
}

こちらがシンプルなHorizontalPagerの使用方法ですpageCountがページ数を設定します。
シンプルに書けるので、Composableのメリットですね!

実行結果

qiita_1.gif

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?