我有一个自定义ViewGroup
,可以在双击时缩放。我想确保这个ViewGroup的孩子只有在我确保触摸是一个单一的点击后才能收到他们的点击事件。不是通过我GestureListener
的{{1}}方法轻击双击手势。
我可以找到许多控制触摸事件传播和教程的教程。拦截他们。我不清楚如何调度点击事件
任何指针?
编辑1
简化的自定义ViewGroup
onSingleTapConfirmed
在我的活动中,我将一些孩子添加到我的自定义视图
public class CustomViewGroup extends ViewGroup {
private final GestureDetector mGestureDetector;
private boolean mGestured = false;
public ViewGroup(Context context) {
mGestureDetector = new GestureDetector(context, new GestureListener());
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(ev);
mGestured = true;
return super.onInterceptTouchEvent(ev);
}
public boolean onTouchEvent(MotionEvent ev) {
if (!mGestured) {
mGestureDetector.onTouchEvent(ev);
mGestured = false;
}
return true;
}
private static class GestureListener extends GestureDetector.SimpleOnGestureListener {
public boolean onDown(MotionEvent e) {
return true;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
// I want to dispatch the onClick event to my children
// (if the event is within their bounds) here
Log.i("custom view group", "on single tap confirmed");
return true;
}
public boolean onDoubleTapEvent(MotionEvent e) {
// Code for double tap zoom
Log.i("custom view group", "on double tap event");
return true;
}
}
}
每当我双击子文本视图顶部时,我都会看到这两个日志语句
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstance) {
TextView textView = new TextView(this);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("textView", "i got clicked: " + v.toString());
}
});
CustomViewGroup customViewGroup = new CustomViewGroup(this);
customViewGroup.addView(textView);
setCustomView(customViewGroup);
}
}
答案 0 :(得分:0)
我不确定它是否可以这样运作。我知道事件会发送到子按钮,然后才能知道是否发生了双击,到时为止告诉孩子不要处理点击是为时已晚。