JHo*_*zer 5 android android-jetpack-compose android-jetpack-compose-list jetpack-compose-accompanist android-jetpack-compose-pager
使用 Jetpack Compose 和伴奏寻呼机,我尝试创建一个HorizontalPager位置:
Pager项目有最大宽度作为一个例子,我编写了以下代码(为了简单起见,我制作了Text物品,但实际上,我实际上制作了更复杂的Card物品):
@Composable
fun MyText(modifier: Modifier) {
Text(
text = LOREM_IPSUM_TEXT,
modifier = modifier
.wrapContentHeight()
.border(BorderStroke(1.dp, Color.Red))
)
}
@ExperimentalPagerApi
@Composable
fun MyPager(pagerItem: @Composable () -> Unit = {}) {
Scaffold {
Column(
modifier = Modifier
.fillMaxSize()
// In case items in the VP are taller than the screen -> scrollable
.verticalScroll(rememberScrollState())
) {
HorizontalPager(
contentPadding = PaddingValues(32.dp),
itemSpacing = 16.dp,
count = 3,
) {
pagerItem()
}
}
}
}
@ExperimentalPagerApi
@Preview
@Composable
fun MyPager_200dpWidth() {
MyPager { MyText(modifier = Modifier.widthIn(max = 200.dp)) }
}
@ExperimentalPagerApi
@Preview
@Composable
fun MyPager_500dpWidth() {
MyPager { MyText(modifier = Modifier.widthIn(max = 500.dp)) }
}
@ExperimentalPagerApi
@Preview
@Composable
fun MyPager_FillMaxWidth() {
MyPager { MyText(modifier = Modifier.fillMaxWidth()) }
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我使项目的最大宽度似乎小于屏幕宽度时(请参阅MyPager_200dpWidth),我不再看到侧面的项目。另一方面,使用最大宽度较大的项目(参见MyPager_500dpWidth)或fillMaxWidth(参见MyPager_FillMaxWidth)可以让我看到侧面的项目。
对我来说,奇怪的是,最大宽度较小的项目实际上比最大宽度较大的项目占用更多的水平空间。这可以在预览部分显示...
看看左侧的图像 ( MyPager_500dpWidth) and middle (MyPager_FillMaxWidth ) show the next item, while the image on the right (MyPager_200dpWidth`) 如何占据整个屏幕?这是将鼠标悬停在物品上以查看骨架框时的另一个比较......
只是想知道是否有人可以帮助我弄清楚如何解决这个用例。是否有可能 Compose 存在错误?
页面大小由HorizontalPager.contentPadding参数控制。
应用于widthIn(max = 200.dp)文本只会减小页面内元素的大小,而页面大小保持不变。
应用更多的填充应该可以解决您的问题,例如,contentPadding = PaddingValues(100.dp)如下所示:
