How To Disable All Child Elements Of View To Capture Mouse Touch Event
I'm trying to Horizontal Slider Menu in Android(like Facebook). I want only my container View to be able to capture mouse touch event. I have tried setEnable(false) all of child
Solution 1:
Override View.onInterceptTouchEvent()
in the ViewGroup
, don't call super.onInterceptTouchEvent()
and return true. This causes the touch events to not be passed down the hierarchy (to the children of the ViewGroup
).
Solution 2:
I have resolved this problem the inspiration of @nmw's answer.
You should extend your View group (I prefer to use LinearLayout
).
For the child elements neglect the mouse touch event. You have to implement onInterceptTouchEvent
method.
This is example layout for solving this problem:
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.LinearLayout;
publicclassMyLinearLayoutextendsLinearLayout {
publicMyLinearLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
publicMyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
booleanmChildCanCaptureTouchEvent=true;
/**
* @return the mChildCanCaptureTouchEvent
*/publicbooleanChildCanCaptureTouchEvent() {
return mChildCanCaptureTouchEvent;
}
/**
* @param mChildCanCaptureTouchEvent
* the mChildCanCaptureTouchEvent to set
*/publicvoidChildCanCaptureTouchEvent(boolean mChildCanCaptureTouchEvent) {
this.mChildCanCaptureTouchEvent = mChildCanCaptureTouchEvent;
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() != MotionEvent.ACTION_MOVE) {
returntrue;
}
if (!mChildCanCaptureTouchEvent)
returntrue;
returnsuper.onInterceptTouchEvent(ev);
}
}
Post a Comment for "How To Disable All Child Elements Of View To Capture Mouse Touch Event"