0

The layout I'm currently using consists of a TabLayout with Viewpager2, what I'm trying to achieve is implementing another ViewPager2 to display several images in one of the tabs. But the issue I'm facing is when I swipe, I am only able to switch between tabs instead of switching images. Any help would be greatly appreciated, thanks!

Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
Noxagon
  • 15
  • 6

1 Answers1

1

Found the solution, just add this piece of code into your program.

private void reduceDragSensitivity() {
    try {
        Field ff = ViewPager2.class.getDeclaredField("mRecyclerView") ;
        ff.setAccessible(true);
        RecyclerView recyclerView =  (RecyclerView) ff.get(viewPager);
        Field touchSlopField = RecyclerView.class.getDeclaredField("mTouchSlop") ;
        touchSlopField.setAccessible(true);
        int touchSlop = (int) touchSlopField.get(recyclerView);
        touchSlopField.set(recyclerView,touchSlop*4);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Remember to replace the viewPager with your own name, if you wish to read more about it: https://medium.com/@al.e.shevelev/how-to-reduce-scroll-sensitivity-of-viewpager2-widget-87797ad02414

Noxagon
  • 15
  • 6