我有一个包含多张全屏图片的画廊。我想将快速滑动手势限制为只能一次前进一张图片(就像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-velocity。也就是说,覆盖onFling,使其看起来像这样:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, velocityX / 4, velocityY);
}
Best,
Michael
你好,我遇到了同样的问题,我使用以下逻辑解决了这个问题。
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它实现了具有“甩”的画廊。