1

I followed this topic auto scroll a Gallery widget to create a Gallery auto scroll from left to right every 5 seconds. Here's my Gallery:

public class MyBannersGallery extends Gallery {

private Handler handler;

public MyBannersGallery(Context ctx, AttributeSet attrSet) {
    super(ctx, attrSet);
    handler = new Handler();
    postDelayedScrollNext();
}

private void postDelayedScrollNext() {
    handler.postDelayed(new Runnable() {
        public void run() {
            postDelayedScrollNext();
            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
        }
    }, 5000);

}

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
    return e2.getX() > e1.getX();
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    int kEvent;
    if (isScrollingLeft(e1, e2)) {
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
    } else {
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
    }
    onKeyDown(kEvent, null);
    return true;
}

}

When it scrolled to the end of my Gallery, it stopped. Now I want to detect if my Gallery is scrolled to the end or not. And if it does, scroll back to the left to the first item. What should I edit my class to archive this?

Community
  • 1
  • 1
user1417127
  • 1,525
  • 1
  • 21
  • 29

2 Answers2

1

Gallery extends from AdapterView, therefore you can use the method 'getSelectedItemPosition()' to determine where the current image index is. So perhaps something like this will work?

private void postDelayedScrollNext() {
    handler.postDelayed(new Runnable() {
        public void run() {
            // check to see if we are at the image is at the last index, if so set the 
            // selection back to 1st image.
            if (getSelectedItemPosition() == getCount() - 1) {
               setSelection(0);
               postDelayedScrollNext();
               return;
            }
            postDelayedScrollNext();

            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
        }
    }, 5000);

}

Of course, this is just an quick hack. If you want nice animation where the gallery scrolls nicely back to the first item, then you have to do additional work, but the idea is the same.

azgolfer
  • 15,087
  • 4
  • 49
  • 46
  • Thank you. I've also just found out the getSelectedItemPosition() method. Thanks for your reply anyway. – user1417127 Jul 17 '12 at 04:47
  • 1
    I'm confused about how the postDelayedScrollNext() works, if it's about auto scrolling every 5s why do we need the onKeyDown(..) –  Oct 01 '13 at 09:07
0

This form work for me:

public MyBannersGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    handler = new Handler();
    postDelayedScrollNext(0);
}

private void postDelayedScrollNext(final int position) {
    handler.postDelayed(new Runnable() {
        public void run() {
             if (getSelectedItemPosition() == getCount() - 1) {
               setSelection(0);
               postDelayedScrollNext(0);
               return;
            }
            setSelection(position+1);
            postDelayedScrollNext(position+1);

        }
    }, 4000);

}