原理:
伪代码
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
var isConsume = false
if (onInterceptTouchEvent(ev)) {
isConsume = onTouchEvent(ev)
} else {
isConsume = child.dispatchTouchEvent(ev)
}
return isConsume
}
外部拦截法:
private var lastX = 0F
private var lastY = 0F
override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
var isIntercept = false
var currentX = e!!.getX()
var currentY = e!!.getY()
when (e.action) {
MotionEvent.ACTION_DOWN -> {
isIntercept = false
}
MotionEvent.ACTION_MOVE -> {
// 根据业务需求,自己实现
isIntercept = abs(currentY - lastY) > abs(currentX - lastX)
}
MotionEvent.ACTION_UP -> {
isIntercept = false
}
}
lastX = currentX
lastY = currentY
return isIntercept
}
内部拦截法:
private var lastX = 0F
private var lastY = 0F
override fun dispatchTouchEvent(e: MotionEvent?): Boolean {
var currentX = e!!.getX()
var currentY = e!!.getY()
when (e.action) {
MotionEvent.ACTION_DOWN -> {
parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_MOVE -> {
// 根据业务需求,自己实现
if (abs(currentY - lastY) > abs(currentX - lastX)) {
parent.requestDisallowInterceptTouchEvent(false)
}
}
MotionEvent.ACTION_UP -> {
}
}
lastX = currentX
lastY = currentY
return super.dispatchTouchEvent(e)
}
|