简体   繁体   English

如何在 Jetpack compose 中检测 HorizontalPager 中的滑动?

[英]How to detect swipe in HorizontalPager in Jetpack compose?

How I can detect when user swiped from one tab to second and etc. in my HorizontalPager()?如何在我的 HorizontalPager HorizontalPager()?

val pagerState = rememberPagerState(initialPage = 0)
HorizontalPager(count = TabCategory.values().size, state = pagerState) { index ->
                                Box(
                                    modifier = Modifier
                                        .fillMaxSize()
                                        .background(MaterialTheme.colors.onBackground)
                                ) {
                                    when (TabCategory.values()[index]) {
                                        TabCategory.Opinion -> { }
                                        TabCategory.Information -> { }
                                        TabCategory.Videos -> { }
                                    }
                                }
                            }

In your viewmodel, pass the pagerState to the HorizontalPager:在您的视图模型中,将 pagerState 传递给 HorizontalPager:

class MyViewModel : ViewModel() {

    val pagerState = PagerState()

    init {
        viewModelScope.launch {
            snapshotFlow { pagerState.currentPage }.collect { page ->
                // Page is the index of the page being swiped.
            }
        }
    }
}

In your composable, use the pagerState:在您的可组合中,使用 pagerState:

HorizontalPager(
  state = myViewModel.pagerState,
) { page ->

}

A PagerState has an InteractionSource that keeps track of that stuff. PagerState有一个跟踪这些内容的InteractionSource The method collectIsDraggedAsState returns a State<Boolean> you can subscribe to that exposes its current value via .value . collectIsDraggedAsState方法返回一个您可以订阅的State<Boolean> ,它通过.value公开其当前值。

This seems to be testing well.这似乎很好地测试了。

@Composable
fun ExampleComposable(){
    val pagerState=rememberPagerState()
    val isDragged=pagerState.interactionSource.collectIsDraggedAsState()
    HorizontalPager(count = injector.snippets.size,
    state = pagerState,
    ){index->
         MyFancyExampleComposable(index)
         if (isDragged.value){
              respondToPageSwipeOrPartialPageSwipe()
            }

     }
}

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

相关问题 如何防止在 Jetpack Compose 中的 HorizontalPager 中滑动 - How to prevent swipe in HorizontalPager in Jetpack Compose 如何在 Jetpack Compose 中禁用 HorizontalPager 的寻呼机 animation - How to disable pager animation of HorizontalPager in Jetpack Compose Jetpack Compose:如何检测 Horizo​​ntalPager 中的 TabRow 何时可见并调用 ViewModel 函数? - Jetpack Compose: How to detect when TabRow inside HorizontalPager is visible and call a ViewModel function? Compose 中 Horizo​​ntalPager 的滑动灵敏度 - Swipe sensitivity for HorizontalPager in Compose 在 Jetpack Compose 上检测滑动方向 - Detect swipe direction on Jetpack Compose Jetpack compose Accompanist HorizontalPager 未检测到滑动 - Jetpack compose Accompanist HorizontalPager doesn't detect swipes 在 Jetpack Compose 的 HorizontalPager 中滚动阻力 - Resistance with scroll in HorizontalPager in Jetpack Compose 带有 LazyColum 的 HorizontalPager 在另一个 LazyColum 中 - Jetpack Compose - HorizontalPager with LazyColum inside another LazyColum - Jetpack Compose Jetpack Compose - 在 Accompanist HorizontalPager 中延迟加载数据 - Jetpack Compose - Lazy loading of data in Accompanist HorizontalPager HorizontalPager 忽略 itemSpacing 并覆盖 Jetpack Compose 中的内容 - HorizontalPager is ignoring itemSpacing and overriding content in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM