歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Gallery滑動太快的問題

Android Gallery滑動太快的問題

日期:2017/3/1 11:12:35   编辑:Linux編程

在做項目時,用Gallery展示圖片,遇到一個問題,就是滑動太快,每次輕輕一撥圖片,就滑動過去幾張,怎麼解決呢?搜索之後,有了下面的解決方法:

1、自定義Gallery重寫onFling方法

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

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,即可

Copyright © Linux教程網 All Rights Reserved