处理viewpager2 嵌套使用滚动冲突的问题

新写的一个项目中因为产品需求,竟然出现了三层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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值