我有ViewPager,在它下面我有10个按钮.通过单击按钮,例如#4,寻呼机立即转到第4页mPager.setCurrentItem(3);.但是,我想通过水平手指滑动来禁用分页.因此,寻呼完成仅通过点击按钮.那么,我怎么能用手指禁用滑动?
使用 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" />
Run Code Online (Sandbox Code Playgroud)
在片段中设置视图
binding.composeView.setContent {
HorizontalScrollableView(listOfCards)
}
Run Code Online (Sandbox Code Playgroud)
以及用 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 …Run Code Online (Sandbox Code Playgroud) android android-viewpager android-jetpack android-jetpack-compose
我有一个嵌套的ViewPager,工作得非常好.唯一的问题是,一旦子ViewPager在最后一项并且我进一步滚动,父ViewPager就会滚动.我不想要这种行为.
我该如何实现这一目标?
这是一个例子,我Activity_main.xml的父ViewPager包含三个片段页面.
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Run Code Online (Sandbox Code Playgroud)
在其中一个片段布局中fragment_categories.xml,我有另一个ViewPager,它是父视图中的子视图Activity_main.xml,
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="@dimen/offer_height"
android:layout_marginTop="2dp"
android:overScrollMode="never"/>
Run Code Online (Sandbox Code Playgroud)
一切正常.但是当子viewPager到达其最后一项时,我滚动更多,父viewPager移动到下一页.我不想要这个.我尝试通过在之前拦截父ViewPager上的触摸来做到这一点,但这不起作用.我猜我做错了?(是的,我在xml中更改了标签com.project.CustomViewPager并试了一下.)
CustomViewPager.java:
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if(v != this && v instanceof ViewPager) {
return false;
}
return super.canScroll(v, checkV, dx, x, y);
}
Run Code Online (Sandbox Code Playgroud) android ×3