Android:ViewGroup,如何拦截MotionEvent然后分派到目标或按需吃掉它?

发布于 2024-11-06 00:44:29 字数 1239 浏览 2 评论 0原文

假设有一个具有多个子项的 ViewGroup。至于这个 ViewGroup,我想让它管理其所有子项的所有 MotionEvent,这表示 VG 将
1. 能够在将所有事件分派给目标(子级)之前拦截所有事件
2. VG会先消费该事件,并判断是否进一步派发事件给目标child
3. DOWN、MOVE、UP,我希望看到它们是相对独立的,这意味着VG可以吃掉DOWN,但把MOVE和UP给孩子。

我读过 SDK 指南“处理 UI 事件”,我知道事件监听器、处理程序、ViewGroup.onInterceptTouchEvent(MotionEvent) 和 View.onTouchEvent(MotionEvent)。

这是我的示例,

@Override
public boolean onInterceptTouchEvent (MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {
        return true;
    }

    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {            
        return true;
    }
    else {
        if (!consumeEvent(event)) {
            // TODO: dispatch to target since I didn't want to eat event
            //return this.dispatchTouchEvent(event);     // infinite loop!!!
        }
    }


    return super.onTouchEvent(event);
}

为了能够处理某些事件,当 DOWN 事件发生时,我必须在上述两种方法中返回 true,因为 SDK 是这么说的。然后我可以在 onTouchEvent 中看到 MOVE 和向上。但是,就我而言,我不知道如何向孩子们发送事件。

上面的dispatchTouchEvent导致了无限循环,这是可以理解的,因为VG本身可能就是目标。我当时不知道哪个是目标,MotionEvent没有给出提示,所以dispatchTouchEvent完全没用。
有人帮我吗?谢谢。

Given that there is a ViewGroup with several children. As for this ViewGroup, I'd like to have it managing all MotionEvent for its all children, which says VG will
1. be able to intercept all events before they get dispatched to target (children)
2. VG will first consume the event, and determine if will further dispatch event to target child
3. DOWN, MOVE, UP, I'd like to see them as relatively independent, which means VG could eat DOWN, but give MOVE and UP to children.

I've read SDK guide "Handling UI Event", I knew event listeners, handlers, ViewGroup.onInterceptTouchEvent(MotionEvent), and View.onTouchEvent(MotionEvent).

Here's my sample,

@Override
public boolean onInterceptTouchEvent (MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {
        return true;
    }

    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {            
        return true;
    }
    else {
        if (!consumeEvent(event)) {
            // TODO: dispatch to target since I didn't want to eat event
            //return this.dispatchTouchEvent(event);     // infinite loop!!!
        }
    }


    return super.onTouchEvent(event);
}

To be able to eat some events, I have to return true in above both methods when DOWN event occurred, because SDK said so. Then I could see MOVE and up in onTouchEvent. However, in my case, I've no idea about how to dispatch event to children.

Above dispatchTouchEvent led to infinite loop, which was understandable, since VG itself might be the target. I can't tell which would be target at that moment, MotionEvent didn't give a hint, so dispatchTouchEvent was totally useless.
Anyone help me out? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2024-11-13 00:44:29

没有简单的方法可以从 onInterceptTouchEvent 找到源 View,也没有办法“调度”这些事件。您可以调度KeyEvent,但不能调度MotionEvent

处理 MotionEvent 的常见方法(例如,拖放)是通过不同的 View 处理 MotionEvent.ACTION_DOWN 事件(通过实现 OnTouchListener 后的 onTouch 回调),以及通过父 ViewgroupMotionEvent.ACTION_MOVE 事件onInterceptTouchEvent 方法。


但有些 LOC 所说的不仅仅是一堆话。我在这里所说的有一个很好的例子: http://doandroids.com/blogs/tag /codeexample/


如果您在视图本身中处理 ACTION_DOWN 事件,那么您可以存储哪个视图在其他地方启动它并使用该变量进行进一步的操作。事件绑定到同一个视图,直到通过 ACTION_UP 或 ACTION_CANCEL 操作完成。

如果您需要在 ACTION_DOWN 期间跟踪 View 并在 ViewGroup 上执行操作,那么我建议您在 ViewGroup 中添加一个公共方法(例如 public boolean handleActionDown (View v, MotionEvent e)将从 onTouch 回调中调用

There is no easy way to find the source View from onInterceptTouchEvent, nor there is a way to "dispatch" these events. You can dispatch KeyEvents, but not MotionEvents.

A common way to deal with MotionEvents (e.g., for drag and drop) is to handle the MotionEvent.ACTION_DOWN events by the different Views (through the onTouch callback after implementing OnTouchListener), and the MotionEvent.ACTION_MOVE events through the parent Viewgroup's onInterceptTouchEvent method.


But some LOCs say a lot more than a bunch of words. There's a very nice example of what I'm saying here: http://doandroids.com/blogs/tag/codeexample/


If you handle the ACTION_DOWN event in the View itself, then you can store which View started it elsewhere and use that variable for further actions. The Event is bound to the same View until is finished by an ACTION_UP or an ACTION_CANCEL actions.

If you need to keep track the View and execute an action on the ViewGroup during the ACTION_DOWN, then I suggest you to add a public method in your ViewGroup (e.g. public boolean handleActionDown (View v, MotionEvent e) that will be called from the onTouch callback

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文