Search code examples
androidandroid-fragmentstouchscrollview

What is the equivalent of dispatchTouchEvent for a fragment?


I'm trying to get horizontal swipe's from a vertical scrollview before they are consumed by the scrollview in order to override them. It can be done in an activity starting with the dispatchTouchEvent.

I'm trying to do the same thing in a ListFragment and a Fragment, but dispatchTouchEvent doesn't seem to exist for fragments. Is there a equivalent method to dispatchTouchEvent in a fragment?


Solution

  • I just put the following in my parent activity in the onCreate ..., then used a public interface like Rarw mentioned above.

            gesturedetector = new GestureDetector(new MyGestureListener());
    
    
        myLayout.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    bTouch = true;
                    return false;
                } else {
                    gesturedetector.onTouchEvent(event);
                    bTouch = false;
                    return true;
                }
            }
    
        }); 
    

    And added the following to my parent activity and a gesturedetector to catch the desired events.

        public boolean dispatchTouchEvent(MotionEvent ev) {
    
        super.dispatchTouchEvent(ev);
    
        return gesturedetector.onTouchEvent(ev);
    
    }