Android Gallery滑动太快的问题


在做项目时,用Gallery展示图片,遇到一个问题,就是滑动太快,每次轻轻一拨图片,就滑动过去几张,怎么解决呢?搜索之后,有了下面的解决方法:

1、自定义Gallery重写onFling方法

  1. public class UGallery extends Gallery {  
  2.   
  3.     public UGallery(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.     }  
  6.   
  7.     private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {  
  8.         return e2.getX() > e1.getX();  
  9.     }  
  10.   
  11.     @Override  
  12.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  13.             float velocityY) {  
  14.         int keyCode;  
  15.         if (isScrollingLeft(e1, e2)) {        
  16.             keyCode = KeyEvent.KEYCODE_DPAD_LEFT;  
  17.         } else {  
  18.             keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;  
  19.         }  
  20.         onKeyDown(keyCode, null);  
  21.         return true;  
  22.     }  
  23. }  

2、在布局文件中使用自定义com.soft.userctrl.UGallery

  1. <com.soft.userctrl.UGallery Android:layout_width="fill_parent" android:spacing="50dip"  
  2.     android:layout_height="fill_parent" android:id="@+id/isMain">  

3、在代码中像使用Gallery一样使用UGallery,即可

 

相关内容