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?
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?
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.