如何限制Android画廊中的投放到每个投放一个项目?

Gun*_*ium 18 android gallery

我有一个带有几个全屏图像的画廊.我想将fling手势限制为一次只推进一个图像(如HTC Gallery应用程序).什么是正确/最简单的方法来实现这一目标?

ole*_*arn 27

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

这将使画廊每次滑动前进一个项目.


小智 20

我有同样的要求,我刚刚发现,如果我只是返回假,它每次只会滑动一个项目.

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                       float velocityY) {        
    return false;
}
Run Code Online (Sandbox Code Playgroud)


Som*_*ere 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 我认为它应该是:else if(velocityX <-1200.0f) (2认同)

小智 7

我有一个解决方案,虽然它不能保证最多一次提前,但是非常简单(并且可能会在代码中手动执行):只需降低onFling参数中的x速度.也就是说,覆盖onFling只是看起来像这样:

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

最好,

迈克尔