2

i am using this calss for detecting swipe gestures, it works fine except with scrollView. either the scrollview or its children absorb my swipes so it fails to catch them.

in my activity

// Swipe
    view = (View) findViewById(R.id.layoutClean);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        }
    });
    view.setOnTouchListener(new OnSwipeTouchListener() {
        public boolean onSwipeRight() {
            Intent intent = new Intent(Clean.this, Tools.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_left_to_right_1,
                    R.anim.slide_left_to_right_2);
            return true;
        }

        public boolean onSwipeLeft() {
            Intent intent = new Intent(Clean.this, Boost.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_right_to_left_1,
                    R.anim.slide_right_to_left_2);
            return true;
        }
    });
    // Swipe

gesture handler

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

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

private final class GestureListener extends SimpleOnGestureListener {

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


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        result = onSwipeRight();
                    } else {
                        result = onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        result = onSwipeBottom();
                    } else {
                        result = onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public boolean onSwipeRight() {
    return false;
}

public boolean onSwipeLeft() {
    return false;
}

public boolean onSwipeTop() {
    return false;
}

public boolean onSwipeBottom() {
    return false;
}

}

MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23

0 Answers0