如何为gridview图像创建滑动动作?

时间:2012-12-18 12:48:52

标签: android image gridview swipe

我想为gridview图像创建fling(滑动)动作。我使用此链接实现了网格视图图像 http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/  在这里,当我点击网格视图图像时,它将转到全屏图像。现在显示完整图像后,用手指触摸从左到右滑动图像。这里不使用视图鳍状肢,因为这里有更多图像。点击哪个图像显示并向右或向左滑动。  感谢

1 个答案:

答案 0 :(得分:0)

为此你可以使用GestureDetector和View.onTouchListener

以下是我之前使用的一些代码的摘录:

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 {
            // Move vertical too much?
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;

            float x1 = e1.getX(),
                  x2 = e2.getX();

            // Right to left swipe
            if (x1 - x2 > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                next();

            // Left to right swipe
            } else if (x2 - x1 > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                previous();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}