使用MotionLayout onswip 可点击的子view不响应滑动

重写MotionLayout,拦截滑动事件


    private var mInitX = 0f
    private var mInitY = 0f

    private var mTouchSlop = 10

        override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        when (ev.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                mInitX = ev.x
                mInitY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {
                val moveX = abs(ev.x - mInitX)
                val moveY = abs(ev.y - mInitY)
                //不拦截横滑事件
                if (moveY > mTouchSlop && moveY > moveX) {

                    val obtain = MotionEvent.obtain(ev)
                    obtain.action = MotionEvent.ACTION_DOWN
                    dispatchTouchEvent(obtain)
                    onTouchEvent(obtain)
                    return true
                }
            }
            MotionEvent.ACTION_UP -> {
            }
        }
        return false
    }

你可能感兴趣的:(使用MotionLayout onswip 可点击的子view不响应滑动)