繁体   English   中英

如何使 ViewPager2 对滑动不那么敏感?

[英]How to make ViewPager2 less sensitive to swipe?

我的ViewPager2包含带有 RecyclerView 的片段。 当我在具有一些项目(不覆盖整个屏幕)的适配器内向下和向上滚动时,它经常“滑动”到视图寻呼机中的下一个或上一个片段。 有什么方法可以降低这个新版本 ViewPager2 的敏感度吗?

<androidx.viewpager2.widget.ViewPager2 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

以下代码适用于 ViewPager2:

    try {
        final Field recyclerViewField = ViewPager2.class.getDeclaredField("mRecyclerView");
        recyclerViewField.setAccessible(true);

        final RecyclerView recyclerView = (RecyclerView) recyclerViewField.get(viewPager);

        final Field touchSlopField = RecyclerView.class.getDeclaredField("mTouchSlop");
        touchSlopField.setAccessible(true);

        final int touchSlop = (int) touchSlopField.get(recyclerView);
        touchSlopField.set(recyclerView, touchSlop * 6);//6 is empirical value
    } catch (Exception ignore) {
    }

暂无
暂无

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

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