其实这个场景很常见,类似SrollView 和ListView等,默写组件本身就需要获得触摸事件,进行处理。如:ListView的header是一个Gallergy ,为了防止冲突,需求当按到header的时候,ListView不获得触摸事件。
实现Activity的dispatchTouchEvent方法
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
int position = nativeListView.getFirstVisiblePosition();
if (position == 0) {
View view = nativeListView.getChildAt(position);
Rect rect = new Rect();
//获取view 相对父容器坐标系统中的位置
view.getHitRect(rect);
//因为本身listView与DectorView也有边距
rect.top+=nativeListView.getTop();
rect.bottom+=nativeListView.getTop();
boolean contains = rect.contains((int) event.getX(),
(int) event.getY());
if (contains) {
target = headerHelper.viewFlow;
} else {
target = null;
}
}
}
if (target != null) {
target.dispatchTouchEvent(event);
if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
target = null;
}
return true;
}
return super.dispatchTouchEvent(event);
}