深色模式
Android 滑动冲突
外部拦截
模板代码:
java
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
//父view拦截条件
boolean condition;
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
intercepted = false;
break;
case MotionEvent.ACTION_MOVE:
if (condition) {
intercepted = true;
} else {
intercepted = false;
}
break;
case MotionEvent.ACTION_UP:
intercepted = false;
break;
}
return intercepted;
}
内部拦截
使用内部拦截需要同时重写父 ViewGroup 的onInterceptTouchEvent
和子 View 的dispatchTouchEvent
方法
父:
java
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
return false;
} else {
return true;
}
}
子:
java
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//父view拦截条件
boolean condition;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
getParent().requestDisallowInterceptTouchEvent(!condition);
break;
case MotionEvent.ACTION_UP:
break;
}
return super.dispatchTouchEvent(event);
}