在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件 [英] Returning false in OnTouch on ImageView but still event is getting consumed

查看:45
本文介绍了在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ImageView.onTouch().我为 ACTION_MOVE 返回 false,但仍然消耗了 onTouch() 事件.

I am using ImageView.onTouch(). I am returning false for ACTION_MOVE but still the onTouch() event is consumed.

imageView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                System.out.println("jan25 iv onTouch");

                if (ev.getActionMasked()==MotionEvent.ACTION_DOWN) {
                    System.out.println("jan25 iv ACTION_DOWN");
                    return true;
                }

                if (ev.getActionMasked()==MotionEvent.ACTION_MOVE){
                    System.out.println("jan25 iv ACTION_MOVE");
                    return false;

                }
                if (ev.getActionMasked()==MotionEvent.ACTION_UP){
                    System.out.println("jan25 iv ACTION_UP");
                    return true;
                }

                System.out.println("jan25 iv false");
                return false;

            }
        });

布局:

 <LinearLayout
        android:id="@+id/ll_magic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/red"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abcd"
             />
    </LinearLayout>

有什么建议

推荐答案

好像view的onTouchEvent()回调返回false只有在接收时才有效ACTION_DOWN 的第一个动作事件.在非消费视图上设置 OnTouchListener 具有相同的语义.这似乎是 ViewGroup 中的 dispatchTouchEvent() 实现中的错误.不过,它似乎不太可能在这么长时间后得到修复,因为这会破坏错误地依赖于这种行为的实现.

It seems that returning false from the onTouchEvent() callback of a view is only effective when receiving the first motion event with ACTION_DOWN. Setting an OnTouchListener on a non-consuming view has the same semantics. This seems to be a bug in the dispatchTouchEvent() implementation in ViewGroup. It doesn't seem likely that it will be fixed after such a long time though, as that would break implementations that incorrectly depend on this behavior.

作为一种解决方法,您可以设置一个标志来指示触摸事件的处理,并在处理之前检查它.此标志应在收到 ACTION_UPACTION_CANCEL 运动事件后重置.

As a workaround, you can set a flag indicating the handling of touch events, and check that before handling. This flag should be reset upon receiving the ACTION_UP or ACTION_CANCEL motion events.

这篇关于在 ImageView 上的 OnTouch 中返回 false,但仍然消耗事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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