How to disable all view child elements to capture mouse touch events

I am trying to use the Horizontal Slider Menu in Android (e.g. Facebook).

I only need my container. View to be able to capture a mouse touch event.

I tried setEnable(false) all the children of my container view. But this leads to the fact that the view does not capture the touch event.

 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); } } } 

What strategy should be used to achieve this goal.

Any help would be appreciated.

0
source share
2 answers

Override View.onInterceptTouchEvent() in the ViewGroup , do not call super.onInterceptTouchEvent() and return true. This leads to the fact that touch events are not passed along the hierarchy (to child ViewGroup elements).

+1
source

I solved this problem by inspiring @nmw's answer.

You should expand the viewing group (I prefer to use LinearLayout ).

For children, ignore the mouse touch event. You must implement the onInterceptTouchEvent method.

This is a sample layout to solve this problem:

import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import 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); } } 
0
source

All Articles