1

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 at all.

This moves image certain distance but does not really implement "true paging".

@Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

          //  return super.onFling(e1, e2, velocityX, velocityY);
            int kEvent;
              if(isScrollingLeft(e1, e2)){ //Check if scrolling left
                kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
              }
              else{ //Otherwise scrolling right
                kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
              }
              onKeyDown(kEvent, null);
              return true;  
        }
        private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
              return e2.getX() > e1.getX();
            }
dropsOfJupiter
  • 6,763
  • 12
  • 47
  • 59

3 Answers3

9

I created new control, called it CustomGallery and extended it from Gallery. In Custom Gallery I placed the following:

@Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         return super.onFling(e1, e2, 0, velocityY);
       }

In my activity I am using CustomGallery instead of Gallery. This works. One thing, we moved from 2.2 to 2.3 (gingerbread). It didn't work for me before when I tried to override onFling. So I am suspecting this also have something to do with the version of OS.

dropsOfJupiter
  • 6,763
  • 12
  • 47
  • 59
  • 2
    wouldn't you think that if you get 333 views that's about the time google start working on a more straightforward solution here? :) – dropsOfJupiter May 10 '11 at 04:34
5

Aniket Awati's solution worked best for me. However I would suggest an improvement to avoid two items beings scrolled in certain cases.

int mSelection = 0;

@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 (mSelection != 0)             
            setSelection(--mSelection, true);
    } else if (leftScroll) {

        if (mSelection != getCount() - 1)
            setSelection(++mSelection, true);
    }
    return false;
}
user1903166
  • 51
  • 1
  • 2
3

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;
}
Aniket Awati
  • 1,381
  • 3
  • 13
  • 21