英文:
How to create an endless Pager in Jetpack Compose
问题
Box(
modifier = Modifier.size(100.dp),
contentAlignment = Alignment.Center
) {
val pagerState = rememberPagerState()
val items = listOf("A", "B", "C")
androidx.compose.foundation.pager.HorizontalPager(
state = pagerState,
pageCount = items.size,
modifier = Modifier,
verticalAlignment = Alignment.CenterVertically
) { page ->
Text(
text = items[page],
modifier = Modifier
)
}
}
在上述代码中,Pager在达到最后一项后停止滚动。然而,我想要Pager在达到最后一项后继续无限滚动。
英文:
Box(
modifier = Modifier.size(100.dp),
contentAlignment = Alignment.Center
) {
val pagerState = rememberPagerState()
val items = listOf("A", "B", "C")
androidx.compose.foundation.pager.HorizontalPager(
state = pagerState,
pageCount = items.size,
modifier = Modifier,
verticalAlignment = Alignment.CenterVertically
) { page ->
Text(
text = items[page],
modifier = Modifier
)
}
}
In the above code, the pager stops scrolling after reaching the last item. However, I want the pager to continue scrolling endlessly after reaching the last item.
答案1
得分: 8
以下是您要翻译的代码部分:
"You can create it by setting pageCount to Int.MAX_VALUE and getting modulus of current page to get index for your list of items.
@Preview
@Composable
private fun Test() {
val pageCount = Int.MAX_VALUE
val items = listOf("A", "B", "C")
val pagerState = rememberPagerState(
initialPage = pageCount / 2
)
HorizontalPager(
modifier = Modifier.fillMaxWidth(),
pageCount = pageCount,
state = pagerState
) {
Text(text = items[it % 3])
}
}"
英文:
You can create it by setting pageCount to Int.MAX_VALUE and getting modulus of current page to get index for your list of items.
@Preview
@Composable
private fun Test() {
val pageCount = Int.MAX_VALUE
val items = listOf("A", "B", "C")
val pagerState = rememberPagerState(
initialPage = pageCount / 2
)
HorizontalPager(
modifier = Modifier.fillMaxWidth(),
pageCount = pageCount,
state = pagerState
) {
Text(text = items[it % 3])
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论