venkat venkat - 5 months ago 68
Android Question

how to create swipe action for gridview images?

I want to create fling(swipe) action for gridview images.I implemented grid view images using this link
http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
here when i clicked grid view images it will goes to full screen images.Now after display full image then swipe images left to right and right to left with finger touch. Here not use view flipper because here more images.Here which image is clicked that is display and swipe to right or left.
thanks

Answer

For that you can use GestureDetector and View.onTouchListener

Here is an extract from some code I used previously:

private int SWIPE_MIN_DISTANCE = 160;
private int SWIPE_MAX_OFF_PATH = 250;
private int SWIPE_THRESHOLD_VELOCITY = 200;
private class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                next();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                previous();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}
Comments