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

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

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

问题

  1. 我有一个HorizontalPager和一个带有两个选项的TabRow目前需要在几乎整个屏幕上滑动才能切换到下一页我想在较小的滚动时也能切换到另一页这是我的当前代码
  2. ```kotlin
  3. val pagerState = rememberPagerState()
  4. val coroutineScope = rememberCoroutineScope()
  5. Column(modifier = Modifier.fillMaxSize()) {
  6. TabRow(
  7. selectedTabIndex = pagerState.currentPage
  8. ) {
  9. Tab(
  10. selected = pagerState.currentPage == 0,
  11. onClick = { component.changeChild(0) },
  12. modifier = Modifier.height(53.dp)
  13. ) {
  14. Column(horizontalAlignment = Alignment.CenterHorizontally) {
  15. Text(
  16. text = "第一页"
  17. )
  18. }
  19. }
  20. Tab(
  21. selected = pagerState.currentPage == 1,
  22. onClick = { component.changeChild(1) },
  23. modifier = Modifier.height(53.dp)
  24. ) {
  25. Column(horizontalAlignment = Alignment.CenterHorizontally) {
  26. Text(
  27. text = "第二页"
  28. )
  29. }
  30. }
  31. }
  32. HorizontalPager(
  33. pageCount = 2,
  34. state = pagerState,
  35. beyondBoundsPageCount = 1,
  36. modifier = Modifier.fillMaxSize()
  37. ) { page ->
  38. when (page) {
  39. 0 -> FirstPage()
  40. 1 -> SecondPage()
  41. }
  42. }

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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"
)
}
}

  1. Tab(
  2. selected = pagerState.currentPage == 1,
  3. onClick = { component.changeChild(1) },
  4. modifier = Modifier.height(53.dp)
  5. ) {
  6. Column(horizontalAlignment = Alignment.CenterHorizontally) {
  7. Text(
  8. text = &quot;Second page&quot;
  9. )
  10. }
  11. }
  12. }
  13. HorizontalPager(
  14. pageCount = 2,
  15. state = pagerState,
  16. beyondBoundsPageCount = 1,
  17. modifier = Modifier.fillMaxSize()
  18. ) { page -&gt;
  19. when (page) {
  20. 0 -&gt; FirstPage()
  21. 1 -&gt; SecondPage()
  22. }
  23. }
  1. 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.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 抱歉,我只会翻译文本内容,不会回答翻译请求。以下是您要翻译的内容:
  6. "Looks like there is something called `positionThresholdFraction` in the `PagerState` class file, but unfortunately it's not changeable for now.
  7. 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."
  8. [1]: https://issuetracker.google.com/issues/289153001
  9. <details>
  10. <summary>英文:</summary>
  11. Looks like there is something called `positionThresholdFraction` in the `PagerState` class file, but unfortunately it&#39;s not changeable for now.
  12. 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.
  13. [1]: https://issuetracker.google.com/issues/289153001
  14. </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:

确定