Gallery scroll one image at a time

后端 未结 3 977
无人及你
无人及你 2021-01-16 10:20

how to make a gallery control to scroll one image at a time? Also what is a good way of making a continuous loop of those images? I tried overriding onFling, does not work a

3条回答
  •  心在旅途
    2021-01-16 10:59

    This works all the time. on all versions without fail for me.

        private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() < e1.getX();
    }
    
    private boolean isScrollingRight(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > e1.getX();
    }
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        boolean leftScroll = isScrollingLeft(e1, e2);
        boolean rightScroll = isScrollingRight(e1, e2);
    
        if (rightScroll) {
            if (getSelectedItemPosition() != 0)             
                setSelection(getSelectedItemPosition() - 1, true);
        } else if (leftScroll) {
    
            if (getSelectedItemPosition() != getCount() - 1)
                setSelection(getSelectedItemPosition() + 1, true);
        }
        return true;
    }
    

提交回复
热议问题