问题描述:
有时候,我们不需要Gallery的惯性,如何去掉Gallery的惯性?
解决方法:
通过继承Gallery,并重写一些方法,自定义Gallery特性。
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;
public class DetailGallery extends Gallery {
public DetailGallery(Context context, AttributeSet attrSet) {
super(context, attrSet);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
@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;
}
}
注意,在xml文件中,使用自定义的DetailGallery代替默认的Gallery。
本文介绍如何通过继承Gallery并重写方法来自定义Gallery特性,以去除其惯性行为。在XML文件中替换默认Gallery为自定义DetailGallery实现。
1206

被折叠的 条评论
为什么被折叠?



