我有一个ViewGroup
容器和其中的一些按钮。
布局如下:
<MyCustomViewGroup>
<Button>
</MyCustomViewGroup>
我需要的是:在我的父视图中检测触摸事件并通过点击显示/隐藏我的按钮。而且我还需要我的按钮单击事件。 我所拥有的-如果我收到按钮点击事件,则父母触摸事件不起作用,如果我检测到父母点击事件,则按钮点击事件不起作用。
对于我的父视图,我有onTouchEvent
和onInterceptTouchEvent
的背景色:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
oldX = ev.getX();
mLastTime = System.currentTimeMillis();
return true;
} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
return true;
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
long timeDifference = (System.currentTimeMillis() - mLastTime);
float moveDifference = Math.abs((oldX - ev.getX()));
if ((moveDifference <= 50) && (timeDifference <= 250L)) {
if (listener != null) {
listener.onTap();
}
}
}
return false;
}
无法找到如何结合使用onInterceptTouchEvent,onTouchEvent和dispatchTouchEvent使其正常工作。