0

I understand Views can decide to handle MotionEvent themselves by returning true in onTouchEvent when being passed a ACTION_DOWN event. If they return true, then they will receive all the subsequent MOVE/UP/CANCEL events. This is working fine.

What I would like to do is defer this decision to the ACTION_MOVE event. I have a ViewGroup containing a child and depending if scrolling horizontally or vertically, I would like the ViewGroup or Child to be responsible for handling the MotionEvents

Something like:

public class MyViewGroup extends ViewGroup{
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            mDownX = event.getX();
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (Math.abs(event.getX() - mDownX) > THRESHOLD_X) {
                // I want all the future events to be forwarded to this ViewGroup
                return true;
            }
        }
        return false;
    }
}

public class MyChild extend View {
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            mDownY = event.getY();
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (Math.abs(event.getY() - mDownY) > THRESHOLD_Y) {
                // I want all the future events to be forwarded to this View
                return true;
            }
        }
        return false;
    }
}

Is that possible ? Right now, only the ViewGroup will receive the MOVE/UP/CANCEL. I guess because it did not find a suitable child to forward the Views to.

Zong
  • 6,160
  • 5
  • 32
  • 46
mbonnin
  • 6,893
  • 3
  • 39
  • 55

1 Answers1

0

For the record, I ended up doing this by overriding ViewGroup.dispatchTouchEvent(). It's a bit cumbersome but doable. I can share the code if needed.

mbonnin
  • 6,893
  • 3
  • 39
  • 55