OnTouchListener MotionEvent.ACTION_MOVE:仅获得第一个视图 [英] OnTouchListener MotionEvent.ACTION_MOVE: gets only first view

查看:182
本文介绍了OnTouchListener MotionEvent.ACTION_MOVE:仅获得第一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有一个按钮网格.当用户在该按钮网格上滑动手指时,我希望所有被触摸的按钮都实质上被记录下来,因此我知道触摸了哪些按钮.

I have a grid of buttons in my activity. When the user slides their finger across this grid of buttons, I want all of the buttons touched to be essentially recorded so I know which buttons were touched.

我一直在对此进行研究,但我看不到如何做到这一点,因为大多数示例都是关于位图的.它选择的第一个视图是唯一从我的OnTouchListener中拾取的视图.从我的阅读中,MotionEvent.ACTION_MOVE是用于此目的的,我看到有人说使用view.getRawX()view.getRawY(),但是我不明白如何将其用于确定按下了哪些其他按钮以及哪些按钮被按下.当用户在屏幕上滑动手指时不是这样.

I have been doing research on this and I do not see how this can be done, as most examples are with Bitmaps. The first view that it selected is the only one that is picked up from my OnTouchListener. From my readings, MotionEvent.ACTION_MOVE is what is to be used for this and I saw someone who said to use view.getRawX() and view.getRawY() but I don't understand how this would be used to determine which other buttons were pressed and which were not when the user is swiping their finger on the screen.

我是Android的新手,所以如果这比我想象的要简单得多,我深表歉意.任何输入将不胜感激,因为我不认为这应该如此复杂.感谢您的时间! :)

I am new to Android so my apologies if this is a much simpler task than I thought. Any input would be much appreciated as I wouldn't think this should be so complicated. Thank you for your time! :)

推荐答案

一旦您的View返回true表示正在使用touch事件,其余视图将不会收到它.您可以做的是创建一个自定义的ViewGroup(您说您有一个网格,我只假设GridView?),该拦截并处理所有触摸事件:

Once your View returns true to say that it's consuming the touch event, the rest of the views won't receive it. What you can do is make a custom ViewGroup (you say you have a grid, I'll just assume GridView?) that intercepts and handles all touch events:

public class InterceptingGridView extends GridView {
    private Rect mHitRect = new Rect();

    public InterceptingGridView (Context context) {
        super(context);
    }

    public InterceptingGridView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        //Always let the ViewGroup handle the event
        return true;
    }

    @Override
    public boolean onTouchEvent (MotionEvent ev) {
        int x = Math.round(ev.getX());
        int y = Math.round(ev.getY());

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.getHitRect(mHitRect);

            if (mHitRect.contains(x, y)) {
                /*
                 * Dispatch the event to the containing child. Note that using this
                 * method, children are not guaranteed to receive ACTION_UP, ACTION_CANCEL,
                 * or ACTION_DOWN events and should handle the case where only an ACTION_MOVE is received.
                 */
                child.dispatchTouchEvent(ev);
            }
        }

        //Make sure to still call through to the superclass, so that
        //the ViewGroup still functions normally (e.g. scrolling)
        return super.onTouchEvent(ev);
    }
}

如何选择处理事件取决于所需的逻辑,但是最重要的是让容器视图使用所有touch事件,并让其处理将事件分派给子级.

How you choose to handle the event depends on the logic that you require, but the takeaway is to let the container view consume all of the touch events, and let it handle dispatching the events to the children.

这篇关于OnTouchListener MotionEvent.ACTION_MOVE:仅获得第一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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