简单测试了一下 requestDisallowInterceptTouchEvent的使用:
两个类:
public static class myView extends View {
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = MotionEventCompat.ACTION_MASK & event.getAction();
Log.e("FYF", "myView " + SystemUtils.motionEventToString(event));
if (action == MotionEvent.ACTION_DOWN) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return true;
}
}
public static class myLinearLayout extends LinearLayout {
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
// return super.onInterceptTouchEvent(ev);
int action = MotionEventCompat.ACTION_MASK & ev.getAction();
if (action == MotionEvent.ACTION_DOWN) {
return false;
} else {
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.e("FYF", "myLinearLayout " + SystemUtils.motionEventToString(event));
return true;
}
}
在MyLinearLayout里放一个myView,
MyLineaLayout在interceptTouchEvevnt时,如果action 是 ACTION_DOWN, 那么就return false,将event继续传递给myView,
否则就自己直接截获,
myView没有requestDisallowInterceptTouchEvent(true):
E/FYF (11126): myView ACTION_DOWN
E/FYF (11126): myView ACTION_CANCEL
E/FYF (11126): myLinearLayout ACTION_MOVE
E/FYF (11126): myLinearLayout ACTION_MOVE
......................................
E/FYF (11126): myLinearLayout ACTION_UP
myView 运行了requestDisallowInterceptTouchEvent(true):
E/FYF (12069): myView ACTION_DOWN
E/FYF (12069): myView ACTION_MOVE
E/FYF (12069): myView ACTION_MOVE
......................................
E/FYF (12069): myView ACTION_UP