Android: How to prevent any touch events from being passed from a view to the one underneath it?

前端 未结 8 868
深忆病人
深忆病人 2021-01-31 13:38

Specifically using the code below, is there a way to modify it so that the activity under this newly created view does not receive any gestures?

View v1 = new Vi         


        
8条回答
  •  没有蜡笔的小新
    2021-01-31 14:02

    Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

    protected boolean onTouchEvent (MotionEvent me) {
        return true;
    }
    

    For v1 you would do an import:

    import android.view.View.OnTouchListener;
    

    Then set the onTouchListener:

    v1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
    

提交回复
热议问题