Returning false in OnTouch on ImageView but still event is getting consumed

邮差的信 提交于 2019-12-20 02:42:53

问题


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;

            }
        });

Layout:

 <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>

Any idea please


回答1:


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.

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.




回答2:


From: http://developer.android.com/guide/topics/ui/ui-events.html

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

So returning false should not consume the event. How did you determine that the event was consumed?




回答3:


This works too, but I'm still not 100% sure what you are asking to do. This sets the imageView clickable to false and should therefore allow the touch not to be consumed by it. Also, I added clickable to the LinearLayout.

 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");
                imageView.setClickable(false);
                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:clickable = "true"
    android:orientation="vertical" >

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


来源:https://stackoverflow.com/questions/28067270/returning-false-in-ontouch-on-imageview-but-still-event-is-getting-consumed

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