【问题标题】:Scroll issues with LazyRow inside traditional android ViewPager在传统的 android ViewPager 中滚动 LazyRow 的问题
【发布时间】:2021-09-03 09:44:07
【问题描述】:

使用 jetpack compose 互操作性 API 时,在预构建的 ViewPager 中使用 LazyRow 会导致滚动问题。

当试图滚动 LazyRow 中的项目时,ViewPager 会移动。有什么办法可以防止这种情况发生吗?

撰写视图

<androidx.compose.ui.platform.ComposeView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/compose_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在片段中设置视图

binding.composeView.setContent {
    HorizontalScrollableView(listOfCards)
}

还有用compose写的视图

@Composable
fun HorizontalScrollableView(listOfCards: List<Cards>) {
    Column(
        modifier = Modifier
            .background(color = colorResource(R.color.grey10))
    ) {
        Text(
            text = "Items",
            color = colors.primary,
            style = typography.subheadBold,
            fontSize = 15.sp,
            modifier = Modifier.padding(start = 16.dp, top = 10.dp, end = 16.dp)
        )
        LazyRow(contentPadding = PaddingValues(end = 16.dp)) {
            items(
                items = listOfCards,
                key = { listOfCards.id }
            ) {
                RenderCard(it)
            }
        }
    }
}

【问题讨论】:

  • 您有关于这个问题的任何消息吗? @akash-amin
  • 我相信最简单的解决方案是从 ViewPager 迁移到 compose Pager 并使用 AndroidViews(或 AndroidViewBindings)作为传统视图。
  • 您使用的是 ViewPager2 还是只使用 ViewPager?如果是 ViewPager2,那么我建议在可行的情况下禁用手动拖动/滚动。
  • 这是伴奏库中的compose pager doc

标签: android android-viewpager android-jetpack android-jetpack-compose


【解决方案1】:

这是您遇到的正常行为。此处描述了类似的问题。

Link

或者,可以控制惰性行手势。

Modifier.pointerInput(Unit) {
    detectTapGestures(
        onPress = { /* Called when the gesture starts */ },
        onDoubleTap = { /* Called on Double Tap */ },
        onLongPress = { /* Called on Long Press */ },
        onTap = { /* Called on Tap */ }
    )
}

在onPress中可以取消你想要的滚动状态

除了作曲,我们也是这样解决的。

Link

我希望这会有所帮助。干得好

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多