用Gallery出現客戶一需求, 說做圖片展示時候,Gallery滑動後,由於慣性原因,會連續切換好幾次,這個就需要改爲滑動一次只切換一個視圖
要實現這個效果就需要去自定義一個Gallery,實現起來還是蠻簡單的,如下
public class OnePageGallery extends Gallery { public OnePageGallery(Context context) { super(context); } public OnePageGallery(Context context, AttributeSet attrs) { super(context, attrs); } public OnePageGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (velocityX > 0) { // 往左邊滑動 super.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null); } else { // 往右邊滑動 super.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null); } return false; } }