Search code examples
javaandroidviewgestureandroid-collapsingtoolbarlayout

Custom gesture detection inside CollapsingToolbarLayout


I'm trying to use a custom view that I've written inside a CollapsingToolbarLayout, but it seems that the touch events are not propagating properly to my custom view with gesture detection. The result is that scrolling and interactions with the view are not working as expected or smoothly. My custom view makes heavy use of GestureDetector.SimpleOnGestureListener

Is it possible to embed a custom view which has it's own touch events inside CollapsingToolbarLayout?


Solution

  • After some investigation, I found that the problem was with onTouchEvent, specifically, you need to requestDisallowInterceptTouchEvent for the parent view so that the paraent view does not process the touch event:

        public boolean onTouchEvent(MotionEvent event) {
          // Do stuff on touch 
          // prevent parent container from processing ACTION_MOVE events
          if(event.getAction() == MotionEvent.ACTION_MOVE) {
            getParent().requestDisallowInterceptTouchEvent(true);
          } else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
            getParent().requestDisallowInterceptTouchEvent(false);
          }
    
        // Do some more stuff
        return true;
    }