简体   繁体   English

在 Compose 中滚动时,HorizontalPager 未占用全宽

[英]HorizontalPager not taking full width while scrolling in Compose

I am using a ComposeView in an xml and loading a HorizontalViewPager in that view.我在ComposeView中使用 ComposeView 并在该视图中加载HorizontalViewPager ntalViewPager。 My expected behaviour of that pager is when I scroll the items of that pager it should not be inside the bound of the padding of the parent layout but take the edge to edge scrolling instead.我对该寻呼机的预期行为是,当我滚动该寻呼机的项目时,它不应位于父布局的填充范围内,而是采用边缘到边缘滚动。 Here is the current look:这是当前的外观: 在此处输入图像描述

But I want something like this:但我想要这样的东西: 在此处输入图像描述

This is how I am getting the viewpager:这就是我获取viewpager的方式:

HorizontalPager(
                count = promotionalBanners.size,
                state = pagerState,
                itemSpacing = 8.dp,
                contentPadding = PaddingValues(end = 16.dp)
            ) {

                Card(
                    Modifier
                        .fillMaxWidth()
                        .height(172.dp)
                ) {
                    NetworkImage(
                        modifier = Modifier
                            .fillMaxSize()
                            .aspectRatio(1.8f)
                            .clip(RoundedCornerShape(4.dp))
                            .clickable {
                                context.openUrl(
                                    promotionalBanners[it].redirectUrl,
                                    navController
                                )
                            },
                        imageUrl = promotionalBanners[it].bannerUrl,
                        contentDescription = promotionalBanners[it].redirectUrl,
                        contentScale = ContentScale.FillBounds
                    )
                }
            }

Can any one suggest me how can I achieve that?任何人都可以建议我如何实现这一目标?

You have to calculate your conentPadding based on your item size.您必须根据您的项目大小计算您的conentPadding

val horizontalPadding = 16.dp
val itemWidth = 340.dp
val screenWidth = LocalConfiguration.current.screenWidthDp
val contentPadding = PaddingValues(start = horizontalPadding, end = (screenWidth - itemWidth + horizontalPadding).dp)

Try this solution.试试这个解决方案。

I have found the solution by myself.我自己找到了解决方案。 Here is how I have solved it.这是我解决它的方法。 In the xml where the composeview is places I have added this:在 composeview 所在的 xml 中,我添加了以下内容:

<androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_promotional_banner"
        android:layout_marginStart="-16dp"
        android:layout_marginEnd="-16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/rec_partners"/>

The -16dp did the trick here, it managed to disobey the parent layout padding, after that the simple below code placed the viewpager in position: -16dp 在这里起到了作用,它成功地违反了父布局填充,然后下面的简单代码将 viewpager 放置在 position 中:

HorizontalPager(
                count = promotionalBanners.size,
                state = pagerState,
                itemSpacing = 8.dp,
                contentPadding = PaddingValues(start = 16.dp, end = 16.dp)
            )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM