2

I have a GridLayout where I would like to intercept swipe events but pass through clicks to child views. Is such a thing possible? Various internet sources suggest to extend GridLayout and override onInterceptTouchEvent, like so:

private static final int minSwipeDistance = 100;
private float lastX;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean response = super.onInterceptTouchEvent(ev);
    final int action = MotionEventCompat.getActionMasked(ev);
    float x = ev.getX();
    switch(action) {
        case MotionEvent.ACTION_DOWN:
            Log.d("ExtendedGrid", "Detected ACTION_DOWN");
            this.lastX = x;
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("ExtendedGrid", "Detected ACTION_MOVE");
            float dx = Math.abs(x - this.lastX);
            if (dx > minSwipeDistance) {
                return true;
            }
            break;
    }
    return response;
}

This extended GridLayout uses the gesture listener found at Android: How to handle right to left swipe gestures

public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public void onSwipeLeft() {
    }

    public void onSwipeRight() {
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_DISTANCE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            Log.d("OnSwipeTouchListener", "Received onDown");
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
}

However, this doesn't capture the ACTION_DOWN, and the gesture listener's onDown() is never called. As a result, onSwipe() has a null first MotionEvent argument and throws an exception.

In the GridLayout code, if I return true for ACTION_DOWN, then the swipe works correctly, but now the child views never get the event and never register clicks. I have also tried directly calling onTouchEvent() myself and returning false, but then I have the original problem where the gesture listener's onDown() is not called.

In summary, is it possible for the extended GridLayout to process the ACTION_DOWN while still passing it on to the child views?

Community
  • 1
  • 1
Katie
  • 21
  • 1

0 Answers0