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)
}
}
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
}