I need to implement a gallery which scrolls one item at a time, and which can do 'autoscroll': i.e. every couple of seconds, it automatically scrolls to the next item.
As per this thread: Android: Programmatically animate between images in Gallery widget, I extended Gallery, overriding onFling to ignore the fling event and instead simulate a DPAD arrow left or right in order to move a single item at a time:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float 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;
}
This works well. For the auto scroll, I create a handler and postDelayed a runnable which simulates the same DPAD key press in the same way:
handler.postDelayed(new Runnable() {
public void run() {
onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
}, AUTO_SCROLL_INTERVAL);
but this doesn't do anything!!
I know the runnable gets executed as expected at the correct interval, since I logged it and confirmed this, but the onKeyDown does not cause the gallery to scroll or switch items. Why would this work in one method and not another? I tried to call my onFling instead, to no avail. Also tried to dispatchKeyEvent, but no luck.
I really need to get this going, but I also really want to understand why the same code doesn't yield the same results in two different places... Does this have something to do with how Android views handle input events? I looked into the Gallery code for clues, but found nothing! It seems as though it should work.
Please, I'm stumped... any ideas?
Thanks