如何在Android相册中限制一次拖动只能滑动一个项目?

18

我有一个包含多张全屏图片的画廊。我想将快速滑动手势限制为只能一次前进一张图片(就像HTC Gallery应用程序一样)。如何正确/最简单地实现这一点?


这里是另一个可能的答案! https://dev59.com/6HE95IYBdhLWcg3wSr-D?lq=1 - Lunf
6个回答

27

只需覆盖 Gallery Widget 的 onFling() 方法,不要调用超类的 onFling() 方法。

这将使画廊在每次滑动时向前移动一个项目。


准确来说,你必须在重写的方法上返回false。 - Andrew Chen

20

我有同样的需求,我刚刚发现如果我返回false,它每次轻扫只会滑动一个项目。

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                       float velocityY) {        
    return false;
}

12

回答该问题的代码示例:

public class SlowGallery extends Gallery
{


    public SlowGallery(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public SlowGallery(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SlowGallery(Context context)
    {
        super(context);
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {

        //limit the max speed in either direction
        if (velocityX > 1200.0f)
        {
            velocityX = 1200.0f;
        }
        else if(velocityX < -1200.0f)
        {
            velocityX = -1200.0f;
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }

}

2
我认为应该是:else if(velocityX < -1200.0f)。 - MataMix

7

我有一个解决方案,虽然它不能保证最多只有一次滑动,但非常简单(并且可能是您在代码中手动执行的操作):只需在onFling参数中减小x-velocity。也就是说,覆盖onFling,使其看起来像这样:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    return super.onFling(e1, e2, velocityX / 4, velocityY);
}

Best,

Michael


这是一个不错的建议,你可以通过一些动画效果来减缓它的速度 :D 比如 /2,/3。 - Sruit A.Suk

3

你好,我遇到了同样的问题,我使用以下逻辑解决了这个问题。

1-> 创建一个类,该类应该扩展自 Gallery
2-> 并重写 onFling 方法。

请参考下面的代码:

package com.sra;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

public class GallView  extends Gallery{
public GallView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

public GallView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }


    public GallView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {        
        return false;
    }

}

在xml中使用这个类作为图库:


<com.sra.GallView
                android:id="@+id/Gallery01"
                android:layout_width="fill_parent"
                android:layout_height="250dip" >
            </com.sra.GallView>

我已经使用它了,它可以工作。现在我该如何将其变成无限画廊呢?我的意思是...最后一张图像之后是第一张图像... - Seshu Vinay

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接