'How to make ViewPager2 less sensitive?

I'm using ViewPager2 in my App, it has three pages and each page show different contents, the issue is that it's too sensible that the ViewPager will change the pages even if someone is scrolling down and for mistake moves the finger a bit to right or left.

So how could i make the ViewPager less sensitive? The ViewPager2 is created in Activity

Here is my code where i initialize the ViewPager:

private lateinit var viewPager: ViewPager2
private lateinit var adapterView: ViewPagerAdapter

private fun initViewPager() {
    adapterView.addFragment(ElencoFragment())
    adapterView.addFragment(TestataFragment())
    adapterView.addFragment(CorpoFragment())
    adapterView.notifyDataSetChanged()

    viewPager.adapter = adapterView
    ...
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewPager = findViewById(R.id.pager)
    adapterView = ViewPagerAdapter(supportFragmentManager, lifecycle)
    ...
}

My Adapter:

 class ViewPagerAdapter
        (manager: FragmentManager, lifecycle: Lifecycle) :
        FragmentStateAdapter(manager, lifecycle) {

        private var fragmentList: ArrayList<Fragment> = ArrayList()

        override fun getItemCount(): Int {
            return fragmentList.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragmentList[position]
        }

        fun addFragment(fragment: Fragment) {
            fragmentList.add(fragment)
        }
    }


Solution 1:[1]

To reduce the sensitivity of the ViewPager's horizontal scroll, try this extension

fun ViewPager2.reduceDragSensitivity(f: Int = 4) {
    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*f)       // "8" was obtained experimentally
}

Source

Solution 2:[2]

this works perfectly in my approach i stored the fragments as an arrayList

inside Activity

val listOfFragment = arrayListOf<Fragment>(
                F1(),
                F2(),
                F3(),
           
            )
            val adapter = TabsPagerAdapter(
                listOfFragment,
                requireActivity().supportFragmentManager,
                requireActivity(),
                0,
                lifecycle

            )

            viewPager1.adapter = adapter           

adapter

class TabsPagerAdapter(val array: ArrayList<Fragment>,
                       val manager: FragmentManager,
                       val context: Context,
                       private val numTabs: Int,
                       lifecycle: Lifecycle): FragmentStateAdapter(manager, lifecycle){
    override fun createFragment(position: Int): Fragment {
        return array[position]
    }

    override fun getItemCount(): Int {
        return array.size
     }
    private fun getReadableTabPosition(position: Int) = position + 1

}

NOTE: the sensitivity might depends on the machine that you are currently running on try switching another devices

Solution 3:[3]

This should be the java code for the kotlin code of @Navjot. Something like sensitivity = 3; or sensitivity = 4; works fine for me.

See soure and other solutions

private void reduceDragSensitivity(int sensitivity) {
    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 * sensitivity);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 c-an
Solution 2
Solution 3 Tobi Z