Android transfer events from child view to parent

I have two custom views of Child (Relative Layout) and Parent (FrameLayout). When a single click occurs, I would like the child process to handle the event, but then still pass it on to the parents, and for any other event in the Child, just pass it on without processing.

The child view is set to match_parent.

This is what I still have:

public class Child extends RelativeLayout implements View.OnTouchListener {
  private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // process the event
        return true;
    }
  }

  public boolean onTouch(View view, MotionEvent motionEvent) {
    return true;
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent e) {
    gestrDetect_.onTouchEvent(e);
    return super.dispatchTouchEvent(e);
  }
}


public class Parent extends FrameLayout {
  public Parent(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnTouchListener(this);
  }

  @Override
  public boolean onTouch(View view, MotionEvent event) {
    return gestrDetect_.onTouchEvent(event);
  }      
}

As long as I get single events in the child view, all other events, such as fling or scale, are not passed to the parent element. What is the correct way to handle a specific subset of events in a child, but still pass some of them to the parent?

Edit

I changed the code to the following:

public class Child extends RelativeLayout implements View.OnTouchListener {
  private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(android.view.MotionEvent e1, android.view.MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // process the event
        return true;
    }
  }

  public boolean onTouch(View view, MotionEvent motionEvent) {
    return gestrDetect_.onTouchEvent(e);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent e) {        
    return super.dispatchTouchEvent(e);
  }
}


public class Parent extends FrameLayout {
  @Override
  public boolean onTouch(View view, MotionEvent event) {
    return gestrDetect_.onTouchEvent(event);
  }      
}

.

@Override
public boolean onDown(MotionEvent e) {
    return true;
}

to

return false;

singleTapUp .

+3
2

ViewGroup onInterceptTouchEvent() ( http://developer.android.com/training/gestures/viewgroup.html).

, .

+2

, .

/**
 * If set to true the layout will discard all received {@link MotionEvent} .
 * As a result none of the children will receive the touch event, instead the parent view will.
 */
public void setPassTouchEventsUp(final boolean passTouchEventsUp) {
    mPassTouchEventsUp = passTouchEventsUp;
}

@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
    if (mPassTouchEventsUp) {
        return true;
    } else {
        return super.onInterceptTouchEvent(ev);
    }
}

@Override
public boolean onTouchEvent(final MotionEvent event) {
    if (mPassTouchEventsUp) {
        return false;
    } else {
        return super.onTouchEvent(event);
    }
}

setPassTouchEventsUp(true), ViewGroup MotionEvent, , /. , "" ViewGroup , .

+1

All Articles