home: reduce viewpager sensitivity

By default, ViewPager2's sensitivity can result in vertical scroll
actions being registered as horizontal scroll actions, which is bad
for UX. Fix that by reflecting into the internal RecyclerView and
changing the touch slope value.
This commit is contained in:
OxygenCobalt 2021-08-22 09:04:54 -06:00
parent b5c5fabad0
commit 49eca55cd7
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47

View file

@ -78,7 +78,32 @@ class HomeFragment : Fragment() {
true true
} }
binding.homePager.adapter = HomePagerAdapter() binding.homePager.apply {
adapter = HomePagerAdapter()
// By default, ViewPager2's sensitivity is high enough to result in vertical
// scroll events being registered as horizontal scroll events. Reflect into the
// internal recyclerview and change the touch slope so that touch actions will
// act more as a scroll than as a swipe.
// Derived from: https://al-e-shevelev.medium.com/how-to-reduce-scroll-sensitivity-of-viewpager2-widget-87797ad02414
try {
val recycler = ViewPager2::class.java.getDeclaredField("mRecyclerView").apply {
isAccessible = true
get(binding.homePager)
}
RecyclerView::class.java.getDeclaredField("mTouchSlop").apply {
isAccessible = true
val slop = get(recycler) as Int
set(recycler, slop * 8)
}
} catch (e: Exception) {
logE("Unable to reduce ViewPager sensitivity")
logE(e.stackTraceToString())
}
}
TabLayoutMediator(binding.homeTabs, binding.homePager) { tab, pos -> TabLayoutMediator(binding.homeTabs, binding.homePager) { tab, pos ->
val labelRes = when (pos) { val labelRes = when (pos) {