在传统的android ViewPager中使用LazyRow滚动问题



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

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

ComposeView

<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)
}

以及撰写中的观点

@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)
}
}
}
}

这是一种正常的行为。这里描述了类似的问题。

链接

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

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中,您可以取消想要的滚动状态

除了作曲,我们还用这种方式解决问题。

链接

我希望这能有所帮助。做得好

这主要是因为ComposeView没有正确实现canScrollHorizontallyViewPager使用这种方法来决定它是否拦截触摸事件。

解决方法:

创建一个扩展FrameLayout的类,重写canScrollHorizontally以始终返回true,并用此容器包装ComposeView。这样,ComposeView中的滚动就可以正常工作。这种解决方法的缺点是,如果你在ComposeView上移动手指,即使ComposeView已经滚动到最后,ViewPager也不会滚动。

最新更新