简体   繁体   中英

android passing events from child view to parent

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

The child view is set to match_parent.

This is what I have so far:

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

While I am getting the single tap up events in the child view, all the other events like fling or scale are not passed to the parent. What is the correct way to handle a certain subset of events in child, while still pass some of them to the parent?

Edit

I have change 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);
  }      
}

And the events are still not passed to the Parent. When I change

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

to

return false;

I am not getting even the singleTapUp event in the child.

You need to create your own implementation of the parent ViewGroup and override onInterceptTouchEvent() method (check out this tutorial http://developer.android.com/training/gestures/viewgroup.html ).

With this you can determine with methods should be handled by parent and which will be passed down to child.

Thing that has worked for me.

/**
 * If set to true the layout will discard all received {@link MotionEvent}'s .
 * 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);
    }
}

When I call setPassTouchEventsUp(true) the ViewGroup will simply not handle any MotionEvent 's, notifying its parent view that the events must be processed by superior views/layouts. This way events "fly through" this ViewGroup up to its parent or whatever there is underneath it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM