【问题标题】:How to disable all Child Elements of View to capture mouse touch event如何禁用视图的所有子元素以捕获鼠标触摸事件
【发布时间】:2013-01-07 05:56:56
【问题描述】:

我正在尝试在 Android(如 Facebook)中使用水平滑块菜单。

我只希望我的容器视图能够捕获鼠标触摸事件。

我已经尝试setEnable(false) 我的容器视图的所有子元素。但它会导致视图无法捕获触摸事件。

public void ChangeMenuVisibility() {
        int menuWidth = menu.getMeasuredWidth();
        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);
        int left = !menuOut ? 0 : menuWidth;
        container.smoothScrollTo(left, 0);
        menuOut = !menuOut;
        ViewUtils.enableDisableViewGroup(
                (ViewGroup) window.findViewById(R.id.main_content), !menuOut);
        window.findViewById(R.id.main_content).setEnabled(true);
    }
[ViewUtils.java]
    public static void enableDisableViewGroup(ViewGroup viewGroup,
            boolean enabled) {
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = viewGroup.getChildAt(i);
            view.setEnabled(enabled);
            if (view instanceof ViewGroup) {
                enableDisableViewGroup((ViewGroup) view, enabled);
            }
        }
    }

我应该遵循什么策略来实现这一点。

任何帮助将不胜感激。

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    覆盖ViewGroup 中的View.onInterceptTouchEvent(),不要调用super.onInterceptTouchEvent() 并返回true。这会导致触摸事件不会向下传递(传递给ViewGroup 的子级)。

    【讨论】:

    • 它不起作用。我扩展了线性布局并实现了 dispatchtouch 事件并返回 true。它不起作用。
    【解决方案2】:

    我已经根据@nmw 的回答解决了这个问题。

    你应该扩展你的视图组(我更喜欢使用LinearLayout)。

    对于子元素忽略鼠标触摸事件。你必须实现onInterceptTouchEvent 方法。

    这是解决此问题的示例布局:

    导入 android.content.Context; 导入 android.util.AttributeSet; 导入 android.view.MotionEvent; 导入android.widget.LinearLayout;

    public class MyLinearLayout extends LinearLayout {
    
        public MyLinearLayout(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        public MyLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
    
        boolean mChildCanCaptureTouchEvent = true;
    
        /**
         * @return the mChildCanCaptureTouchEvent
         */
        public boolean ChildCanCaptureTouchEvent() {
            return mChildCanCaptureTouchEvent;
        }
    
        /**
         * @param mChildCanCaptureTouchEvent
         *            the mChildCanCaptureTouchEvent to set
         */
        public void ChildCanCaptureTouchEvent(boolean mChildCanCaptureTouchEvent) {
            this.mChildCanCaptureTouchEvent = mChildCanCaptureTouchEvent;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (ev.getAction() != MotionEvent.ACTION_MOVE) {
                return true;
            }
    
            if (!mChildCanCaptureTouchEvent)
                return true;
    
            return super.onInterceptTouchEvent(ev);
        }
    }
    

    【讨论】:

    • 您不需要删除,因为我会选择您的答案为真。我只是为了一个示例演示而写的......
    猜你喜欢
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2020-04-10
    • 2023-03-29
    相关资源
    最近更新 更多