简体   繁体   English

HorizontalPager 忽略 itemSpacing 并覆盖 Jetpack Compose 中的内容

[英]HorizontalPager is ignoring itemSpacing and overriding content in Jetpack Compose

I have a HorizontalPager (from Accompanist) with a currentPage focus effect.我有一个带有 currentPage 焦点效果的 HorizontalPager(来自 Accompanist)。 On the emulator, it works correctly and with the correct spacing that I intend, however, on a smartphone, it doesn't respect the itemSpacing and completely ignores it, overlapping the pager items on the right.在模拟器上,它可以正常工作,并且具有我想要的正确间距,但是,在智能手机上,它不遵守 itemSpacing 并完全忽略它,与右侧的寻呼机项目重叠。 Is there any way to make this pager always have the same spacing regardless of the device?有没有办法让这个寻呼机始终具有相同的间距,而不管设备如何? Or, at least, that the item on the right does not overlap and that the main item overlaps the other two?或者,至少,右边的项目不重叠并且主要项目与其他两个项目重叠?

Below a few screenshots of what is happening and the behavior that I want:下面是正在发生的事情和我想要的行为的一些屏幕截图:

Correct behavior (working on emulator):正确的行为(在模拟器上工作):

在此处输入图像描述 在此处输入图像描述

Wrong behavior & problem (working on physical device):错误行为和问题(在物理设备上工作):

在此处输入图像描述

Code:代码:

 HorizontalPager(
        state = state,
        count = items.count(),
        contentPadding = PaddingValues(horizontal = 72.dp),
        itemSpacing = 10.dp,
        modifier = modifier.padding(top = 16.dp)
    ) { page ->
        Card(
            shape = RectangleShape,
            modifier = Modifier
                .graphicsLayer {
                    // Calculate the absolute offset for the current page from the
                    // scroll position. We use the absolute value which allows us to mirror
                    // any effects for both directions
                    val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue

                    // We animate the scaleX + scaleY, between 85% and 100%
                    lerp(
                        start = 0.85f,
                        stop = 1f,
                        fraction = 1f - pageOffset.coerceIn(0f, 1f)
                    ).also { scale ->
                        scaleX = scale
                        scaleY = scale
                    }

                    // We animate the alpha, between 50% and 100%
                    alpha = lerp(
                        start = 0.6f,
                        stop = 1f,
                        fraction = 1f - pageOffset.coerceIn(0f, 1f)
                    )
                }
                .requiredWidth(284.dp)
                .requiredHeight(168.dp)
                //.coloredShadow(Shadow, 0.28f)
        ) {
            // content
        }
    }

Any additional necessary code can be provided, just let me know.可以提供任何其他必要的代码,请告诉我。

You should be using how to support different screen sizes in android with jetpack compose, in which you can define dimensions for different configurations.您应该使用 how to support different screen sizes in android with jetpack compose,您可以在其中为不同的配置定义尺寸。 You can refer to this tutorial on the same同上可以参考这个教程

https://proandroiddev.com/supporting-different-screen-sizes-on-android-with-jetpack-compose-f215c13081bd https://proandroiddev.com/supporting-different-screen-sizes-on-android-with-jetpack-compose-f215c13081bd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jetpack Compose 的 HorizontalPager 中滚动阻力 - Resistance with scroll in HorizontalPager in Jetpack Compose 如何在 Jetpack compose 中检测 HorizontalPager 中的滑动? - How to detect swipe in HorizontalPager in Jetpack compose? 如何在 Jetpack Compose 中禁用 HorizontalPager 的寻呼机 animation - How to disable pager animation of HorizontalPager in Jetpack Compose 带有 LazyColum 的 HorizontalPager 在另一个 LazyColum 中 - Jetpack Compose - HorizontalPager with LazyColum inside another LazyColum - Jetpack Compose Jetpack Compose:忽略后代的内容描述 - Jetpack Compose: ignoring descendants' content description Jetpack Compose - 在 Accompanist HorizontalPager 中延迟加载数据 - Jetpack Compose - Lazy loading of data in Accompanist HorizontalPager 如何防止在 Jetpack Compose 中的 HorizontalPager 中滑动 - How to prevent swipe in HorizontalPager in Jetpack Compose Jetpack Compose - 具有最大宽度的项目的 Horizo​​ntalPager 项目间距/填充 - Jetpack Compose - HorizontalPager item spacing/padding for items with max width Jetpack compose Accompanist HorizontalPager 未检测到滑动 - Jetpack compose Accompanist HorizontalPager doesn't detect swipes 在 Jetpack Compose 中使用 HorizontalPager + LazyVerticalGrid 时性能不佳 - Performance is bad when using HorizontalPager + LazyVerticalGrid in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM