Closed wangtonggen closed 2 years ago
外层viewPager使用的是viewPager2
这两个结合使用应该会造成冲突的
same issue,我做了 viewpager2 hook 处理,把其内部的 recyclerView touchSlop 修改了,也算是一种方式吧。代码如下:Kotlin
step1 define extension
/**
* Reduces drag sensitivity of [ViewPager2] widget
* [ Support nested scrolling elements with the same scroll direction.](https://issuetracker.google.com/issues/123006042)
*/
fun ViewPager2.reduceDragSensitivity() {
try {
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 * 4)
} catch (e: Exception) {
// failed to change:(
}
}
step2 use in page
viewpager2.reduceDragSensitivity()
same issue ,这里我借助Google处理ViewPager2嵌套冲突的思路 对NestedScrollableHost.kt 进行了改造,并解决了这一问题,NestedScrollableHost的使用参考googles示例
源代码 private val child: View? get() = if (childCount > 0) getChildAt(0) else null
修改后 private val child: View? get() = when { childCount > 0 && getChildAt(0) is XBanner -> (getChildAt(0) as XBanner).viewPager childCount > 0 -> getChildAt(0) else -> null }
viewPager可以滑动 xbanner不能滑动