更改在Jetpack Compose中的HorizontalPager中切换到下一页之前的滚动距离

huangapple go评论57阅读模式
英文:

Change scroll distance before going to next page for HorizontalPager in Jetpack Compose

问题

我有一个HorizontalPager和一个带有两个选项的TabRow目前需要在几乎整个屏幕上滑动才能切换到下一页我想在较小的滚动时也能切换到另一页这是我的当前代码

```kotlin
val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()

Column(modifier = Modifier.fillMaxSize()) {
    TabRow(
        selectedTabIndex = pagerState.currentPage
    ) {
        Tab(
            selected = pagerState.currentPage == 0,
            onClick = { component.changeChild(0) },
            modifier = Modifier.height(53.dp)
        ) {
            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Text(
                    text = "第一页"
                )
            }
        }

        Tab(
            selected = pagerState.currentPage == 1,
            onClick = { component.changeChild(1) },
            modifier = Modifier.height(53.dp)
        ) {
            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Text(
                    text = "第二页"
                )
            }
        }
    }


    HorizontalPager(
        pageCount = 2,
        state = pagerState,
        beyondBoundsPageCount = 1,
        modifier = Modifier.fillMaxSize()
    ) { page ->
        when (page) {
            0 -> FirstPage()
            1 -> SecondPage()
        }
    }

我尝试过修改HorizontalPager中的flingBehavior,但我尝试过的所有方法都没有修改所需的滑动距离。我还在网上查找过,但所有的问题要么是针对旧版本的Accompanist库中的HorizontalPager,要么是针对Snapper库的。


<details>
<summary>英文:</summary>

I have a HorizontalPager with a TabRow with two items. Currently you need to swipe almost the entire screen before it snaps to the next page. I&#39;d like it to snap to the other page on a smaller scroll. This is my current code:

val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()

Column(modifier = Modifier.fillMaxSize()) {
TabRow(
selectedTabIndex = pagerState.currentPage
) {
Tab(
selected = pagerState.currentPage == 0,
onClick = { component.changeChild(0) },
modifier = Modifier.height(53.dp)
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(
text = "First page"
)
}
}

    Tab(
        selected = pagerState.currentPage == 1,
        onClick = { component.changeChild(1) },
        modifier = Modifier.height(53.dp)
    ) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(
                text = &quot;Second page&quot;
            )
        }
    }
}


HorizontalPager(
    pageCount = 2,
    state = pagerState,
    beyondBoundsPageCount = 1,
    modifier = Modifier.fillMaxSize()
) { page -&gt;
    when (page) {
        0 -&gt; FirstPage()
        1 -&gt; SecondPage()
    }
}

I&#39;ve tried to modify the `flingBehavior` in HorizontalPager, but nothing I&#39;ve tried actually modifies the distance needed to swipe. I&#39;ve also looked online, but all the questions are either for the old HorizontalPager from the Accompanist library, or the [Snapper](https://chrisbanes.github.io/snapper/) library.

</details>


# 答案1
**得分**: 1

抱歉,我只会翻译文本内容,不会回答翻译请求。以下是您要翻译的内容:

"Looks like there is something called `positionThresholdFraction` in the `PagerState` class file, but unfortunately it's not changeable for now.
I have created an issue, you can find it [right here][1]. Feel free to click +1 right under the link so google will react faster."

[1]: https://issuetracker.google.com/issues/289153001

<details>
<summary>英文:</summary>

Looks like there is something called `positionThresholdFraction` in the `PagerState` class file, but unfortunately it&#39;s not changeable for now.
I have created an issue, you can find it [right here][1]. Feel free to click +1 right under the link so google will react faster.


  [1]: https://issuetracker.google.com/issues/289153001

</details>



huangapple
  • 本文由 发表于 2023年5月14日 18:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定