Android如何让Gallery自动滚动到第一项?
我按照这个主题创建了一个画廊自动滚动从左到右每5秒。这是我的画廊:Android如何让Gallery自动滚动到第一项?,android,scroll,gallery,Android,Scroll,Gallery,我按照这个主题创建了一个画廊自动滚动从左到右每5秒。这是我的画廊: public class MyBannersGallery extends Gallery { private Handler handler; public MyBannersGallery(Context ctx, AttributeSet attrSet) { super(ctx, attrSet); handler = new Handler(); postDelayedScrollNext(
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扩展而来,因此您可以使用“GetSelectEditePosition()”方法来确定当前图像索引的位置。那么,也许这样的事情会奏效
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);
}
当然,这只是一个快速破解。如果你想要一个漂亮的动画,其中库可以很好地滚动回到第一个项目,那么你必须做额外的工作,但想法是一样的。库从AdapterView扩展而来,因此你可以使用方法“GetSelectEditePosition()”来确定当前图像索引的位置。那么,也许这样的事情会奏效
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);
}
当然,这只是一个快速破解。如果你想要一个好的动画,画廊可以很好地滚动回到第一个项目,那么你必须做额外的工作,但想法是一样的。这个表单适合我:
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);
}
此表格适用于我:
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);
}
非常感谢。我还发现了getSelectEditePosition()方法。谢谢你的回复。我对postDelayedScrollNext()的工作原理感到困惑,如果是每5秒自动滚动一次,为什么我们需要onKeyDown(…)谢谢。我还发现了getSelectEditePosition()方法。不管怎样,谢谢你的回复。我对postDelayedScrollNext()的工作原理感到困惑,如果它是关于每5秒自动滚动一次,为什么我们需要onKeyDown(…)