在做項目時,用Gallery展示圖片,遇到一個問題,就是滑動太快,每次輕輕一撥圖片,就滑動過去幾張,怎麼解決呢?搜尋之後,有了下面的解決方案:
1、自訂Gallery重寫onFling方法
public class UGallery extends Gallery {
public UGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int keyCode;
if (isScrollingLeft(e1, e2)) {
keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(keyCode, null);
return true;
}
}
2、在布局檔案中使用自訂com.soft.userctrl.UGallery
<com.soft.userctrl.UGallery android:layout_width="fill_parent" android:spacing="50dip"
android:layout_height="fill_parent" android:id="@+id/isMain">
</com.soft.userctrl.UGallery>
3、在代碼中像使用Gallery一樣使用UGallery,即可
摘自:心靈淨土的專欄