我开发了一个自定义的 gallery,然后重写了 on-fling方法,每次刷新一个图像。能运行,但是现在的问题是当我从上到下刷新或者从下到上时,图像就刷新了,因此也改变了。
public class mygallery extends Gallery {
public mygallery(Context ctx, AttributeSet attrSet) {
super(ctx, attrSet);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
private boolean isScrollingRight(MotionEvent e1, MotionEvent e2){
return e2.getX() < e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int kEvent=0;
if (isScrollingLeft(e1, e2)) { // Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else if(isScrollingRight(e1, e2)) { // Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
}
要怎么做才能当我从上到下刷新或者从下到上时,图像不改变?
如果你想在用户从上到下或者从下到上时,不改变图像,你可以在 onFling 方法中加入判断条件,如果手势是从上到下或者从下到上时,就不调用 onKeyDown 方法。
下面是一个例子:
private boolean isScrollingUp(MotionEvent e1, MotionEvent e2) {
return e2.getY() < e1.getY();
}
private boolean isScrollingDown(MotionEvent e1, MotionEvent e2) {
return e2.getY() > e1.getY();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int kEvent = 0;
if (isScrollingLeft(e1, e2)) { // Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else if (isScrollingRight(e1, e2)) { // Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
} else if (isScrollingUp(e1, e2) || isScrollingDown(e1, e2)) {
// Do nothing
}
onKeyDown(kEvent, null);
return true;
}
这样,当用户从上到下或者从下到上时,就不会调用 onKeyDown 方法,图像也就不会改变了。