简体   繁体   English

onTouchListener始终获得相同的MotionEvent ACTION_UP

[英]onTouchListener always getting the same MotionEvent ACTION_UP

At first i always get the ACTION_DOWN mesage so i googled and i found that i have to return TRUE at the end. 起初,我总是收到ACTION_DOWN消息,所以我用谷歌搜索,发现最后必须返回TRUE。 So i changed it and i started to get always ACTION_UP. 所以我更改了它,然后我开始总是得到ACTION_UP。 I dont understand why. 我不明白为什么。

linearLayoutDraggable.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //ConstraintLayout.LayoutParams par = (ConstraintLayout.LayoutParams)v.getLayoutParams();
                CardView.LayoutParams par = (CardView.LayoutParams)v.getLayoutParams();


                switch (event.getAction()) {

                    case MotionEvent.ACTION_UP: {
                        //par.height = 40;
                        Toast.makeText(MagMainNewActivity.this, "UP", Toast.LENGTH_SHORT).show();
//                                par.height=300;
//                                par.topMargin = (int) event.getRawY() - (v.getHeight());
//                                par.leftMargin = (int) event.getRawX() - (v.getWidth() / 2);
//                                v.setLayoutParams(par);

                        break;
                    } //inner case UP
                    case MotionEvent.ACTION_DOWN: {
                        Toast.makeText(MagMainNewActivity.this, "DOWN", Toast.LENGTH_SHORT).show();
//                                par.height = 115;
//                                //par.width = 60;
//                                v.setLayoutParams(par);
                        break;
                    } //inner case UP
                } //inner switch

                return true;
            }
        });

The onTouchListener receive motion gestures, so (while returning true) if you receive a ACTION_DOWN, you will receive each ACTION_MOVE finalized with only ONE ACTION_UP that means the pointer (finger) was removed from the screen at that point. onTouchListener会接收动作手势,因此(返回true时),如果您收到ACTION_DOWN,则将收到仅以一个ACTION_UP完成的每个ACTION_MOVE终结,这意味着此时已将指针(手指)从屏幕上移开了。

A click action is represented from a ACTION_DOWN plus a ACTION_UP, while a ACTION_DOWN without a UP is a long press. 点击动作是从ACTION_DOWN加ACTION_UP表示的,而没有UP的ACTION_DOWN则是长按。

Maybe you are seeing just the UP message from the toast since both events are trigged sequentially and the second toast overrided the first one, if you add Log.v("MMNA", "DOWN") you can check both at the Logcat 也许您只是从烤面包上看到了UP消息,因为这两个事件是按顺序触发的,而第二个烤面包则覆盖了第一个,如果添加Log.v("MMNA", "DOWN")您可以在Logcat上同时检查

Because before finger is up finger down is first, this means MotionEvent.ACTION_DOWN is came frist. 因为在手指抬起之前首先是手指放下,所以这意味着MotionEvent.ACTION_DOWN首先出现。 It check event action and if finger touch a screen action is MotionEvent.ACTION_DOWN and it break. 它检查事件动作,如果手指触摸屏幕动作为MotionEvent.ACTION_DOWN并中断。 Sorry for bad English 对不起英语不好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM