ViewPager2 多层(两层以上)嵌套滑动

797 阅读2分钟

Jetpack 团队在 ViewPager2 的 Sample 写过一个例子,就是解决多层 ViewPager2 滑动时事件冲突的问题NestedScrollableHost,但是这个在三层及以上的嵌套上会出现一些问题,当最底层的ViewPager2 执行到边缘时,取消父控件拦截操作会导致中间层的ViewPager2的滑动失效,直接执行最外层的滑动逻辑。因此我在其基础上添加了,逐层取消父控件拦截操作的逻辑,完善了多层冲突情况的解决,下面直接放源码:

class NestedScrollableHost  : FrameLayout {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    private var touchSlop = 0
    private var initialX = 0f
    private var initialY = 0f

    private val parentViewPager: ViewPager2?
        get() {
            var v: View? = parent as? View
            while (v != null && v !is ViewPager2) {
                v = v.parent as? View
            }
            return v as? ViewPager2
        }


    private val newstedScrollHostList:ArrayList<NestedScrollableHost>
        get(){
            var v:View?=parent as? View
            val array= arrayListOf<NestedScrollableHost>()
            while (v!=null){
                if(v is NestedScrollableHost){
                    array.add(v)
                }
                v = v.parent as? View
            }
            return array
        }

    private val child: View? get() = if (childCount > 0) getChildAt(0) else null

    init {
        touchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    private fun canChildScroll(orientation: Int, delta: Float): Boolean {
        val direction = -delta.sign.toInt()
        return when (orientation) {
            0 -> child?.canScrollHorizontally(direction) ?: false
            1 -> child?.canScrollVertically(direction) ?: false
            else -> throw IllegalArgumentException()
        }
    }

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        handleInterceptTouchEvent(e)
        return super.onInterceptTouchEvent(e)
    }



    private fun handleInterceptTouchEvent(e: MotionEvent) {
        val orientation = parentViewPager?.orientation ?: return
        // Early return if child can't scroll in same direction as parent
        if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) {
            return
        }

        if (e.action == MotionEvent.ACTION_DOWN) {
            initialX = e.x
            initialY = e.y
            parent.requestDisallowInterceptTouchEvent(true)
        } else if (e.action == MotionEvent.ACTION_MOVE) {
            val dx = e.x - initialX
            val dy = e.y - initialY
            val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
            // assuming ViewPager2 touch-slop is 2x touch-slop of child
            val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f
            val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
            if (scaledDx > touchSlop || scaledDy > touchSlop) {

                if (isVpHorizontal == (scaledDy > scaledDx)) {
                    // Gesture is perpendicular, allow all parents to intercept
                    parent.requestDisallowInterceptTouchEvent(false)
                } else {
                    // Gesture is parallel, query child if movement in that direction is possible
                    if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) {
                        // Child can scroll, disallow all parents to intercept
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        // Child cannot scroll, allow all parents to intercept
                        parent.requestDisallowInterceptTouchEvent(false)
                        if(newstedScrollHostList.size>0){
                            //当多层嵌套时,最底层无法滑动后,逐级往上判断是否可以滑动
                            newstedScrollHostList[newstedScrollHostList.size-1].setRequestDisallowInterceptTouchEventForTrue()
                        }
                    }
                }
            }
        }
    }

    private fun setRequestDisallowInterceptTouchEventForTrue(){
        parent.requestDisallowInterceptTouchEvent(true)
    }
}