Mouse move on two views, how to let both of them handle the `ACTION_MOVE` event

旧巷老猫 提交于 2019-12-06 07:03:54

问题


I have two views, the bigger one is on the bottom, and the smaller one is on the top. Show on following picture:

Now I press mouse on view1 (outside view2), then move to view2. I found even if the mouse is inside view2 during the movement, view2 won't get the ACTION_MOVE event. Only view1 can get it.

What I want:

When the mouse is inside view1 and outside view2, let view1 handle the ACTION_MOVE event. If the mouse move into view2, then let view2 handle the ACTION_MOVE event.

How to do it?

PS: android version is 2.x


回答1:


It is not possible,but you can do some things else:

Get coordinates of mouse and view2,if mouse is in bounds of view2,try what you want.Or if you want,you can call view2.onTouch method directly,create a touchlistener class for V2,for example V2TouchListener then:

View v2 = ... ;
final V2TouchListener v2t = new V2TouchListener();
v2.setOnTouchListener(v2t);
v1.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            // do something
            break;
        case MotionEvent.ACTION_MOVE:
            // do something
            //if mouse is in bounds of view2 do this:
            //for example view is between x= 20 and x = 50
            if((event.getX() < 50) &&(event.getX() > 20) )
            v2t.onTouch(v2, event);
            break;
        }

        return false;
    }
});


来源:https://stackoverflow.com/questions/13530104/mouse-move-on-two-views-how-to-let-both-of-them-handle-the-action-move-event

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