安卓:ViewGroup中,如何拦截MotionEvent,然后派遣到目标还是吃它的需求呢? [英] Android: ViewGroup, how to intercept MotionEvent and then dispatch to target or eat it on demand?

查看:207
本文介绍了安卓:ViewGroup中,如何拦截MotionEvent,然后派遣到目标还是吃它的需求呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有几个孩子的ViewGroup。至于这个ViewGroup中,我想拥有它管理所有MotionEvent其所有的孩子,它说VG将结果
1.能够拦截所有的事件,它们会派遣到目标(儿童)结果之前,
2. VG将首先消耗的情况,并决定是否将进一步dispatch事件为目标的孩子结果
3.向下,移动,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以上。然而,在我的情况,我没有关于如何事件分派到孩子的想法。

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 ,也有一种方法来调度这些事件。你可以派遣的KeyEvent S,而不是 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.

一个普遍的方式来处理 MotionEvent (例如,拖放)是处理 MotionEvent.ACTION_DOWN 由不同的查看 S(通过实施后的 onTouch 回调 OnTouchListener ),并通过父母的的ViewGroup MotionEvent.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.

但一些交通线说比一堆话多了不少。有说什么我在这里的一个很好的例子: http://doandroids.com/博客/标签/ 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/

如果您处理视图本身的ACTION_DOWN事件,那么你可以存储在其他地方查看它开始并使用该变量的进一步行动。事件被绑定到了同样的看法,直到由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过程中执行的ViewGroup中的动作,那么我建议你在你的ViewGroup(如公共布尔handleActionDown(视图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

这篇关于安卓:ViewGroup中,如何拦截MotionEvent,然后派遣到目标还是吃它的需求呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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