Android 台灣中文網

標題: android怎麼檢測滾動結束 [打印本頁]

作者: 暗桌之光    時間: 2011-6-25 11:38
標題: android怎麼檢測滾動結束
  1. // declare class member variables
  2. private GestureDetector mGestureDetector;
  3. private OnTouchListener mGestureListener;
  4. private boolean mIsScrolling = false;


  5. public void initGestureDetection() {
  6.         // Gesture detection
  7.     mGestureDetector = new GestureDetector(new SimpleOnGestureListener() {
  8.         @Override
  9.         public boolean onDoubleTap(MotionEvent e) {
  10.             handleDoubleTap(e);
  11.             return true;
  12.         }

  13.         @Override
  14.         public boolean onSingleTapConfirmed(MotionEvent e) {
  15.             handleSingleTap(e);
  16.             return true;
  17.         }

  18.         @Override
  19.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  20.             // i'm only scrolling along the X axis
  21.             mIsScrolling = true;               
  22.             handleScroll(Math.round((e2.getX() - e1.getX())));
  23.             return true;
  24.         }

  25.         @Override
  26.         /**
  27.          * Don't know why but we need to intercept this guy and return true so that the other gestures are handled.
  28.          * https://code.google.com/p/android/issues/detail?id=8233
  29.          */
  30.         public boolean onDown(MotionEvent e) {
  31.             Log.d("GestureDetector --> onDown");
  32.             return true;
  33.         }
  34.     });

  35.     mGestureListener = new View.OnTouchListener() {
  36.         public boolean onTouch(View v, MotionEvent event) {

  37.             if (mGestureDetector.onTouchEvent(event)) {
  38.                 return true;
  39.             }

  40.             if(event.getAction() == MotionEvent.ACTION_UP) {
  41.                 if(mIsScrolling ) {
  42.                     Log.d("OnTouchListener --> onTouch ACTION_UP");
  43.                     mIsScrolling  = false;
  44.                     handleScrollFinished();
  45.                 };
  46.             }

  47.             return false;
  48.         }
  49.     };

  50.     // attach the OnTouchListener to the image view
  51.     mImageView.setOnTouchListener(mGestureListener);
  52. }
複製代碼





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1