繁体   English   中英

在伴奏寻呼机中自动播放图像

[英]Autoplay Images in Accompanist Pager

我正在使用 Accompanist 实现一个水平寻呼机。 有没有一种优雅的方式每 5 秒自动切换一次图像?

否则,我必须通过在视图模型中使用一个每 5 秒增加一次 currentPage 的通道来伪造手动滑动。老实说,我不是很喜欢。

在 Compose 之前,我曾经实现了Why Not Image Carousel ,它具有内置的自动播放属性。

任何帮助,将不胜感激。 谢谢!

您可以执行以下操作:

val images = // list of your images...

val pageState = rememberPagerState(pageCount = images.size)
HorizontalPager(state = pageState) {
  // Your content...
}

// This is the interesting part, when your page changes,
// the LaunchedEffect will run again
LaunchedEffect(pageState.currentPage) {
    delay(3000) // wait for 3 seconds.
    // increasing the position and check the limit
    var newPosition = pageState.currentPage + 1
    if (newPosition > images.lastIndex) newPosition = 0
    // scrolling to the new position.
    pageState.animateScrollToPage(newPosition)
}

这是结果(在 GIF 中,间隔为 1 秒):

在此处输入图片说明

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM