Mic*_*lsx 6 android android-viewpager2
我的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" />
Run Code Online (Sandbox Code Playgroud)
以下代码适用于 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) {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1420 次 |
| 最近记录: |