简体   繁体   English

JetPack Compose Pager 的 onPageSelected 回调

[英]onPageSelected callback for JetPack Compose Pager

I am using JetPack Compose Pager from Accompanist and I'm wonder how do I know exactly when my page is Showed at screen.我正在使用 Accompanist 的 JetPack Compose Pager ,我想知道我如何确切知道我的页面何时显示在屏幕上。 Like onPageSelected method from ViewPager.就像ViewPager的 onPageSelected 方法一样。

Here is my code:这是我的代码:

HorizontalPager(
        state = pagerState,
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colors.background)
    ) { page ->
         // This method reinvoked many times.

      }

'Cause currently each recomposition would invoke that callback method from Pager.因为目前每个重组都会从寻呼机调用该回调方法。

I think it will be better to use我认为使用会更好

LaunchedEffect(pagerState) {
    snapshotFlow { pagerState.currentPage }.collect { page ->
        // do your stuff with selected page
    }
}

Figured out!想通了! We can use LaunchedEffect to know when your page would be successfully shown.我们可以使用LaunchedEffect来了解您的页面何时会成功显示。

HorizontalPager(
    state = pagerState,
    modifier = Modifier.fillMaxSize()
) { page ->
     LaunchedEffect(key1 = page, block = {
       if (!pagerState.isScrollInProgress) {
          // Here you are
       }
     })
  }

LaunchedEffect invoked only once per key (which is current page). LaunchedEffect 每个键(即当前页面)仅调用一次。 So, with this code you can achieve onPageSelected feature.因此,使用此代码您可以实现onPageSelected功能。

Use a variable to keep track使用变量来跟踪

val trigger by remember { mutableStateOf (false) }
HorizontalPager(
        state = pagerState,
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colors.background)
    ) { page ->
         // Shown successfully,
         trigger = true //use at other places as a callback to change state
      }

To complete @ysfcyln solution here is the official documentation :要完成@ysfcyln 解决方案,这里是官方文档:

https://google.github.io/accompanist/pager/#reacting-to-page-changes https://google.github.io/accompanist/pager/#reacting-to-page-changes

the full example there is :完整的例子是:

val pagerState = rememberPagerState()

LaunchedEffect(pagerState) {
    // Collect from the a snapshotFlow reading the currentPage
    snapshotFlow { pagerState.currentPage }.collect { page ->
        AnalyticsService.sendPageSelectedEvent(page)
    }
}

VerticalPager(
    count = 10,
    state = pagerState,
) { page ->
    Text(text = "Page: $page")
}

We can do our tasks without any callbacks using PagerState<\/code> .我们可以使用PagerState<\/code>完成我们的任务而无需任何回调。

val pagerState = rememberPagerState(pageCount = list.size)
        
        HorizontalPager(state = pagerState) { page ->
            // Our page content
            CardItemView(page, list[page])
        }

// Do your tasks based on the current page selected using pagerState.currentPage
        Toast.makeText(
            LocalContext.current,
            "Page selected ${pagerState.currentPage}",
            Toast.LENGTH_SHORT
        ).show()

This is a solution simpler than the one from the official documentation这是一个比官方文档中的解决方案更简单的解决方案

LaunchedEffect(pagerState.currentPage) {
        // do your stuff with pagerState.currentPage
}

Here is my solution which worked quite well for me.这是我的解决方案,对我来说效果很好。 Extra thing with in it was "distinctUntilChanged"其中的额外内容是“distinctUntilChanged”

LaunchedEffect(pagerState) {
    snapshotFlow {
        pagerState.currentPage
    }.distinctUntilChanged().collect { page ->
        // Now you can use selected page
        onPageChangeListener.invoke(page)
    }
}

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

相关问题 如何在 Jetpack Compose 中禁用 HorizontalPager 的寻呼机 animation - How to disable pager animation of HorizontalPager in Jetpack Compose Jetpack Compose 水平寻呼机滑动延迟 - Jetpack Compose Horizontal Pager Swipe Delay Jetpack Compose 中带有 Paging3 的水平寻呼机 - Horizontal Pager with Paging3 in Jetpack Compose Jetpack 撰写:在可比较的寻呼机中滑动页面时出现问题 - Jetpack compose : Problem while swiping pages in pager compasable Jetpack Compose - 通过可组合回调传递 object - Jetpack Compose - pass an object through composable callback Jetpack compose:BottomSheet 状态更改回调 - Jetpack compose : BottomSheet state change callback Jetpack Compose:使用 Coil 完成图像加载后获取回调 - Jetpack Compose: Get a callback when the image has finished loading with Coil 如何使用带有 addOnNewIntentListener 回调的 Jetpack Compose ComponentActivity 的广播 Intent - How to use a broadcasted Intent with Jetpack's Compose ComponentActivity with addOnNewIntentListener callback 有没有办法在 jetpack-compose 伴奏水平寻呼机上禁用右水平滚动? - Is there a way to disable right horizontal scrolling on jetpack-compose accompanist horizontal pager? Jetpack Compose:Jetpack Compose 中的优雅文本高度 - Jetpack Compose: elegantTextHeight in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM