Android: can't see ACTION_MOVE/UP in onTouchEvent of a RelativeLayout

半城伤御伤魂 提交于 2020-01-13 08:14:06

问题


I registered a listener to a RelativeLayout, see below. I'd like to add some custom event handling,

mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            final int action = motionEvent.getAction();
            boolean ret = false;

            switch (action) {
            case MotionEvent.ACTION_DOWN:
                ret = doSth(motionEvent);
                break;
            case MotionEvent.ACTION_MOVE:
                ret = doSth(motionEvent);
                break;
            case MotionEvent.ACTION_UP:
                ret = doSth(motionEvent);
                break;
            }

            return ret;     // returning false got me none of MOVE/UP events right here
        }
    };

However, I can't get any MOVE/UP events unless returned true.

Another try, I registered same listener to a CheckBox, everything went quite well.
Is there difference between ViewGroup and Widget? Design purpose?


回答1:


"However, I can't get any MOVE/UP events unless returned true."

You've answered your own question. If you don't return true indicating that you care about and are handling the event, the system will not send you the subsequent events, because you've told the system you don't care about it. If you need to monitor the entire life cycle of the touch event, you need to return true always.



来源:https://stackoverflow.com/questions/5933426/android-cant-see-action-move-up-in-ontouchevent-of-a-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!