[英]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.