Search code examples
androideventsgesturemotion

"Nephew" views block pinch gesture


I have a hierachy of custom views build as following:

  • Custom View A has a few View B's as children
  • Custom View B is like a button and needs to react to click/tap
  • Custom View C has a pinch gesture on it and is a sibling of A, and is under A in the view hierarchy

Schema of views to explain the case

When pinching only on the Custom View A, everything goes well, as A does not catch any motion event and the view under it, C, receives the events and makes the pinch gesture (zoom in/out) correctly.

When tapping the Custom View B, the view is clicked and it is all good.

The issue is, when trying to pinch and one finger starts on View B, it catches its events and those are not propagated under it and no pinching ever happens on the Custom View C.

I would like to have View B react to tap only and pass all other events (pinch) to the view underneath itself, in this case Custom View C.

How can I achieve that scenario?


Solution

  • Found the solution!

    We try to find a TapConfirmed event (using a gestureDetector to find it), and we dispatch the event directly to the sibling to pass the event:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
    
    
        if (conditionToIntercept) {
            return true;
        }
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        if(conditionToIntercept{
            if (!gestureDetectorCompat.onTouchEvent(event)) {
                siblingView.dispatchTouchEvent(event);
                return true;
            }
        }
    
        return false;
    }