How can I disable all touch events on all children of a ViewGroup?

前端 未结 2 1155
囚心锁ツ
囚心锁ツ 2020-12-29 11:33

I\'ve got a FrameLayout container containing many things (including ScrollView, WebView, ViewPager...).

I would l

相关标签:
2条回答
  • 2020-12-29 11:48

    You could disable them like this:

    FrameLayout parent = (FrameLayout)findViewById(some_id);
    disableChildsOnTouch(parent)
    
    
    public void disableChildsOnTouch(ViewGroup viewGroup){
        int cnt = viewGroup.getChildCount();
        for (int i = 0; i < cnt; i++){
            View v = viewGroup.getChildAt(i);
            if (v instanceof ViewGroup){
                disableChildsOnTouch((ViewGroup)v);
            } else {
                v.setOnTouchListener(null);
                v.setOnClickListener(null);
                //v.SETYOURLISTENER(null)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 11:58

    May be I have misunderstood something, but I guess the answer to your question is very simple: If you don't want to dispatch none of the touch events to the children - so you just need to override ViewGroup.onInterceptTouchEvent (MotionEvent ev) API reference like this:

    public boolean onInterceptTouchEvent (MotionEvent ev){
        return true;
    }
    

    According to the android documentation this means that your ViewGroup will always intercept all the touch events and don't dispatch them to the children. All of them will be directed to the ViewGroup.onTouchEvent(MotionEvent ev) method of your ViewGroup.

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