我按照主题auto scroll a Gallery widget进行创建,每5秒钟从左到右创建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;
}
}
当它滚动到我的画廊的尽头时,它停止了。现在,我想检测我的图库是否滚动到末尾。如果是这样,请向左滚动到第一项。我应该编辑我的班级以归档此内容吗?
最佳答案
Gallery从AdapterView扩展而来,因此您可以使用方法“ getSelectedItemPosition()”来确定当前图像索引在哪里。那么也许这样的事情会起作用吗?
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);
}
当然,这只是一个快速的技巧。如果您想要精美的动画,其中图库可以很好地滚动回到第一项,那么您必须做其他工作,但是想法是一样的。