繁体   English   中英

如何将 Android 库中的投掷限制为每次投掷仅一项?

[英]How can I limit fling in Android gallery to just one item per fling?

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

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

这将使图库每次滑动前进一项。

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

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

回答问题的代码示例:

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);
    }

}

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

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

最好的事物,

迈克尔

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

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>

我找不到任何限制滚动的方法,但我解决了实现/适应此代码的一些成功问题: http : //permalink.gmane.org/gmane.comp.handhelds.android.devel/101327

它实现了一个带有“fling”的画廊

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM