如果单纯的直接用Gallery来实现View的切换,往往给人一闪而过的现象,为了解决这个问题,我们重写一下Gallery,直接简单的重写几个方法,就能解决这个问题。
下面是代码:
public class GuideGallery extends Gallery {
public GuideGallery(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public GuideGallery(Context context,AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public GuideGallery(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
System.out.println("左滑");
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
System.out.println("右滑");
}
onKeyDown(kEvent, null);
return true;
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
}
这样就实现了每次滑动一个View的效果。