自动滚动窗口小部件画廊 [英] auto scroll a Gallery widget
问题描述
我需要实现它一次滚动一个项目一个画廊,并能做到自动滚屏:即每隔几秒钟,它会自动滚动到下一个项目
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.
根据此线程:<一href=\"http://stackoverflow.com/questions/2731564/android-programmatically-animate-between-images-in-gallery-widget\">Android:在图库部件图像之间的动画编程,我扩展库,覆盖的 onFling 以忽略一扔事件,而是模拟DPAD箭头向左或向右,以一次移动单个项目:
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;
}
这工作得很好。
对于自动滚屏,我创建了一个处理程序,并postDelayed一个可运行它模拟同样的DPAD键preSS以同样的方式:
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!!
我认识的获取可运行于正确的间隔期望,因为我记录它,并证实了这一执行,但在的onkeydown 不会导致画廊滚动或切换项目。
为什么会这样工作在一个方法而不是其他?我试着打电话给我的 onFling 代替,无果。还试图 dispatchKeyEvent ,但没有运气。
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.
我真的需要得到这个打算,但我也真的想明白为什么同样的code不会产生在两个不同的地方相同的结果......这是否有东西做的Android如何处理意见输入事件?我看着画廊code线索,却一无所获!它好像它应该工作。
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?
感谢
推荐答案
所以,事实证明,原来的逻辑,我的确实的工作。
自定义库实现该逻辑,但剥夺我班上其他一切(下同)正常工作,所以它必须是别人在我班上的东西,是造成这个问题。
So, it turns out that the original logic I had does work. A custom gallery implementing that logic but stripped of everything else in my class (below) works as expected, so it must be something else in my class that is causing this problem.
感谢您的帮助HighFlyer:)
Thanks for your help HighFlyer :)
至少,我学到了反思......这是Java的非常酷的功能。
At the very least I learned about reflection... which is a very cool feature of Java.
public class CustomGallery extends Gallery {
private Handler handler;
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
handler = new Handler();
postDelayedScrollNext();
}
private void postDelayedScrollNext() {
handler.postDelayed(new Runnable() {
public void run() {
postDelayedScrollNext();
Log.d("CustomGallery", "dpad RIGHT");
onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
}, 1000);
}
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)) {
Log.d("CustomGallery", "fling LEFT");
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
Log.d("CustomGallery", "fling LEFT");
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
这篇关于自动滚动窗口小部件画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!