Help, how to avoid oblique swipe in viewpager2
I have a screen with elements divided into pages when effect a horizontal or vertical swipe is ok
but when the user makes an oblique swipe the page changes
how to avoid this?
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager2.widget.ViewPager2 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And i try with this
binding.page.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> binding.page.isUserInputEnabled = false
}
return v?.onTouchEvent(event) ?: true
}
})
I have the same problem, this answer cant help me
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
}
I just tested out the behavior you described. In my opinion ViewPager2
and RecyclerView
work together flawlessly: On a diagonal swipe with less than 45°, the ViewPager2
gets scrolled. On diagonal swipes with more than 45°, the RecyclerView
gets scrolled.
However, this seems only true when the Activity
is newly created. It also seems to depend a little bit on the prior scroll gesture.
RecyclerView
gets scrolled), after that, diagonal scrolls again will scroll the RecyclerView
.ViewPager2
gets scrolled), then diagonal scrolls tend to scroll the ViewPager2
. Maybe this is the intended behavior. To provide a more intuitive user experience,
I'd probably keep it as is, as I had to test quite some time to reproduce your described behavior.
Actually, all the scrolling felt very intuitive the way it is.
And keep in mind: If you really only want to allow exactly 0° horizontal swipes to scroll the ViewPager2
, you're giving your users a very hard time. It would be practically impossible to scroll the ViewPager2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.