Android:ViewGroup,如何拦截MotionEvent然后派发到目标或者按需吃掉? [英] Android: ViewGroup, how to intercept MotionEvent and then dispatch to target or eat it on demand?

查看:19
本文介绍了Android:ViewGroup,如何拦截MotionEvent然后派发到目标或者按需吃掉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于有一个 ViewGroup 有几个孩子.至于这个 ViewGroup,我想让它为它的所有孩子管理所有 MotionEvent,这表示 VG 将
1. 能够在所有事件被分派到目标(孩子)之前拦截所有事件
2. VG会先消费事件,再判断是否进一步派发事件给目标子
3. DOWN, MOVE, UP,我想看他们相对独立,也就是说VG可以吃DOWN,但是给孩子MOVE和UP.

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.

我已阅读 SDK 指南处理 UI 事件",我知道事件侦听器、处理程序、ViewGroup.onInterceptTouchEvent(MotionEvent) 和 View.onTouchEvent(MotionEvent).

I've read SDK guide "Handling UI Event", I knew event listeners, handlers, ViewGroup.onInterceptTouchEvent(MotionEvent), and 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 和 up.但是,就我而言,我不知道如何将事件发送给孩子.

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.

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

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.

推荐答案

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

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.

处理MotionEvents(例如拖放)的常用方法是通过不同的ViewMotionEvent.ACTION_DOWN事件处理code>s(通过实现OnTouchListener后的onTouch回调),以及通过父ViewgroupMotionEvent.ACTION_MOVE事件> 的 onInterceptTouchEvent 方法.

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.

但有些 LOC 说的远不止一堆词.这里有一个很好的例子:http://doandroids.com/blogs/tag/codeexample/

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/

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

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.

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

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

这篇关于Android:ViewGroup,如何拦截MotionEvent然后派发到目标或者按需吃掉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆