英文:
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'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 = "Second page"
)
}
}
}
HorizontalPager(
pageCount = 2,
state = pagerState,
beyondBoundsPageCount = 1,
modifier = Modifier.fillMaxSize()
) { page ->
when (page) {
0 -> FirstPage()
1 -> SecondPage()
}
}
I've tried to modify the `flingBehavior` in HorizontalPager, but nothing I've tried actually modifies the distance needed to swipe. I'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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论