How can I disable all views inside the layout?

后端 未结 23 1349
一个人的身影
一个人的身影 2020-11-28 08:46

For example I have:



        
23条回答
  •  庸人自扰
    2020-11-28 09:36

    Although not quite the same as disabling views within a layout, it is worth mentioning that you can prevent all children from receiving touches (without having to recurse the layout hierarchy) by overriding the ViewGroup#onInterceptTouchEvent(MotionEvent) method:

    public class InterceptTouchEventFrameLayout extends FrameLayout {
    
        private boolean interceptTouchEvents;
    
        // ...
    
        public void setInterceptTouchEvents(boolean interceptTouchEvents) {
            this.interceptTouchEvents = interceptTouchEvents;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return interceptTouchEvents || super.onInterceptTouchEvent(ev);
        }
    
    }
    

    Then you can prevent children from receiving touch events:

    InterceptTouchEventFrameLayout layout = (InterceptTouchEventFrameLayout) findViewById(R.id.layout);
    layout.setInterceptTouchEvents(true);
    

    If you have a click listener set on layout, it will still be triggered.

提交回复
热议问题