Gallery去除惯性、半屏翻页

来源:互联网 发布:北京php招聘 编辑:程序博客网 时间:2024/06/10 08:43

问题描述:

有时候,我们不需要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();}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {int kEvent;if (isScrollingLeft(e1, e2)) {// Check if scrolling leftkEvent = KeyEvent.KEYCODE_DPAD_LEFT;} else {// Otherwise scrolling rightkEvent = KeyEvent.KEYCODE_DPAD_RIGHT;}onKeyDown(kEvent, null);return true;}}


注意,在xml文件中,使用自定义的DetailGallery代替默认的Gallery。

原创粉丝点击