ViewPager2 嵌套Fragment 左右滑动过于灵敏问题

思路:通过反射修改设置 vp2的灵敏度:

以下为Kotlin写法、放在vp2 相关的activity页面(onCreate 函数中)

  //动态设置ViewPager2 灵敏度
        try {
            val recyclerViewField: Field = ViewPager2::class.java.getDeclaredField("mRecyclerView")
            recyclerViewField.isAccessible = true
            val recyclerView = recyclerViewField.get(viewPager) as RecyclerView
            val touchSlopField: Field = RecyclerView::class.java.getDeclaredField("mTouchSlop")
            touchSlopField.isAccessible = true
            val touchSlop = touchSlopField.get(recyclerView) as Int
            touchSlopField.set(recyclerView, touchSlop * 7) //6 is empirical value
            Log.d("xxxxx", "testOver")
        } catch (ignore: java.lang.Exception) {
        }

你可能感兴趣的:(android,开发,android,java,Viewpager2)