【问题标题】:Use custom viewgroup to control click event flows使用自定义视图组来控制点击事件流
【发布时间】:2015-02-27 23:10:06
【问题描述】:

我有一个自定义的ViewGroup,可以通过双击进行缩放。我想确保只有在我通过我的GestureListeneronSingleTapConfirmed 确保触摸是单击而不是双击手势的第一次轻击之后,这个 ViewGroup 的子级才会收到他们的点击事件方法。

我可以找到很多教程来控制触摸事件传播和拦截它们。我不清楚调度点击事件是如何工作的

任何指针?

编辑 1

简化的自定义视图组

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);
    }
}

每当我双击子文本视图顶部时,我都会看到这两个日志语句

02-27 15:12:24.343  23837-23909/com.asdf.qw I/textView﹕ i got clicked
02-27 15:12:24.343  23837-23909/com.asdf.qw I/custom view group﹕ on double tap event

【问题讨论】:

  • 能否将相关代码添加到您的问题中?

标签: android viewgroup


【解决方案1】:

我不确定它是否可以这样工作。我知道在您知道是否发生双击之前,事件被发送到子按钮,到那时告诉孩子不要处理点击为时已晚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多