Gallery中,取消慣性滑動,滑動一次只切換一個視圖

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



發佈了32 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章