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

前端 未结 3 682
南旧
南旧 2021-01-20 04:08

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

imageV         


        
相关标签:
3条回答
  • 2021-01-20 04:22

    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.

    0 讨论(0)
  • 2021-01-20 04:35

    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>
    
    0 讨论(0)
  • 2021-01-20 04:36

    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?

    0 讨论(0)
提交回复
热议问题