新写的一个项目中因为产品需求,竟然出现了三层viewpager2嵌套的问题,也就是viewpager2套fragment,fragment里面的viewpager2再套fragment,fragment里面再套一层viewpager2,三层viewpager2嵌套,简直要搞死我最终还是解决了,代码如下
class NestedScrollableHost constructor(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
private var touchSlop = 0
private var initialX = 0f
private var initialY = 0f
init {
val var10001 = ViewConfiguration.get(context)
this.touchSlop = var10001.scaledTouchSlop
}
private fun getParentViewPager(): ViewPager2? {
var mViewParent = this.parent
if (mViewParent !is View) {
mViewParent = null
}
var view: View?
view = mViewParent as View
while (view != null && view !is ViewPager2) {
mViewParent = view.parent
if (mViewParent !is View) {
mViewParent = null
}
view = if (mViewParent == null) null else mViewParent as View
}
val var2: View? = if (view !is ViewPager2) null else view
return if (var2 == null) null else var2 as ViewPager2
}
private fun getChild(): View? {
return if (this.childCount > 0) getChildAt(0) else null
}
private fun canChildScroll(orientation: Int, delta: Float): Boolean {
val var5 = false
val direction = -sign(delta).toInt()
val var10000: View?
var var6 = false
when (orientation) {
0 -> {
var10000 = getChild()
var6 = var10000?.canScrollHorizontally(direction) ?: false
}
1 -> {
var10000 = getChild()
var6 = var10000?.canScrollVertically(direction) ?: false
}
else -> {
}
}
return var6
}
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
this.handleInterceptTouchEvent(ev)
return super.onInterceptTouchEvent(ev)
}
private fun handleInterceptTouchEvent(e: MotionEvent) {
val var10000 = getParentViewPager()
if (var10000 != null) {
val orientation = var10000.orientation
if (canChildScroll(orientation, -1.0f) || canChildScroll(orientation, 1.0f)) {
when (e.action) {
MotionEvent.ACTION_DOWN -> {
initialX = e.x
initialY = e.y
this.parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_MOVE -> {
val dx = e.x - initialX
val dy = e.y - initialY
val isVpHorizontal = orientation == 0
val var8 = false
val scaledDx = abs(dx) * if (isVpHorizontal) 0.5f else 1.0f
val var9 = false
val scaledDy = abs(dy) * if (isVpHorizontal) 1.0f else 0.5f
if (scaledDx > touchSlop.toFloat() || scaledDy > touchSlop.toFloat()) {
when {
isVpHorizontal == scaledDy > scaledDx -> {
this.parent.requestDisallowInterceptTouchEvent(false)
}
canChildScroll(orientation, if (isVpHorizontal) dx else dy) -> {
this.parent.requestDisallowInterceptTouchEvent(true)
}
else -> {
this.parent.requestDisallowInterceptTouchEvent(false)
}
}
}
}
}
}
}
}
}
使用也很简单,用此自定义view嵌套viewpage2即可
<com.test.view.NestedScrollableHost
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.virtual.ailover.ui.view.NestedScrollableHost>