viewpager2降低了水平滚动的灵敏度



如何降低viewpager2中水平滚动的灵敏度?

这是我当前用于视图寻呼机的代码。

viewPager = findViewById(id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(this, dataManager, USERNAME));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.green));
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(tabNames.get(position));
}
}).attach();

我一直在寻找同样的东西,并在Kotlin:中找到了这个解决方案

val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
recyclerViewField.isAccessible = true
val recyclerView = recyclerViewField.get(this) as RecyclerView
val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
touchSlopField.isAccessible = true
val touchSlop = touchSlopField.get(recyclerView) as Int
touchSlopField.set(recyclerView, touchSlop*8)       // "8" was obtained experimentally

如果你需要Java,它应该是这样的:

try {
Field recyclerViewField = ViewPager2.class.getDeclaredField("mRecyclerView");
recyclerViewField.setAccessible(true);
RecyclerView recyclerView = (RecyclerView) recyclerViewField.get(myViewPager);
Field touchSlopField = RecyclerView.class.getDeclaredField("mTouchSlop");
touchSlopField.setAccessible(true);
int touchSlop = (int) touchSlopField.get(recyclerView);
touchSlopField.set(recyclerView, touchSlop * 8);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}

这不是一个"好"的解决方案,因为它使用了反射API,但它应该会有所帮助。

最新更新