繁体   English   中英

如何用 Jetpack compose 实现轮播(卡片滑块)?

[英]How to implement carousel (card slider) with Jetpack compose?

我一直在使用 这个库在 View 系统中实现轮播。 如何在 Jetpack Compose 中实现轮播?

嗯...这取决于您需要从该库中获得的功能,但对于“简单”寻呼机,您可以使用 Pager Accompanist 库。

https://github.com/google/accompanist/tree/main/pager

您可以在此处查看文档:

https://google.github.io/accompanist/pager/

尝试这种方式使用HorizontalPager

@Composable
fun BuildLoginSlider() {
  val pagerState = rememberPagerState(initialPage = 1)
  val sliderList = listOf()

  HorizontalPager(
    count = sliderList.size, state = pagerState,
    contentPadding = PaddingValues(horizontal = 150.dp),
    verticalAlignment = Alignment.CenterVertically,
  ) { page ->
    Card(
      colors = CardDefaults.cardColors(Color.Transparent),
      shape = RoundedCornerShape(10.dp),
      elevation = CardDefaults.cardElevation(0.dp),
      modifier = Modifier
        .graphicsLayer {

          val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue

          lerp(
            start = 0.85f,
            stop = 1f,
            fraction = 1f - pageOffset.coerceIn(0f, 1f)
          ).also { scale ->
            scaleX = scale
            scaleY = scale
          }
          alpha = lerp(
            start = 0.5f,
            stop = 1f,
            fraction = 1f - pageOffset.coerceIn(0f, 1f)
          )
        }
      // .aspectRatio(0.5f)
    ) {
      AsyncImage(
        model = Builder(LocalContext.current)
          .data(sliderList[page])
          .crossfade(true)
          .scale(FILL)
          .build(),
        contentDescription = null,
        modifier = Modifier
          .offset {
            // Calculate the offset for the current page from the
            // scroll position
            val pageOffset =
              this@HorizontalPager.calculateCurrentOffsetForPage(page)
            // Then use it as a multiplier to apply an offset
            IntOffset(
              x = (70.dp * pageOffset).roundToPx(),
              y = 0,
            )
          }
      )
    }

  }
}

OUTPUT

在此处输入图像描述

暂无
暂无

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

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