Skip to content Skip to sidebar Skip to footer

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;

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

Post a Comment for "How To Disable All Child Elements Of View To Capture Mouse Touch Event"