1. 程式人生 > >Android中手勢滑動翻頁之GestureDetector總結

Android中手勢滑動翻頁之GestureDetector總結

寫作緣由:最近在做左右滑動翻頁效果,程式碼寫出來經過3天除錯毫無進展,經過網上多次搜尋和自己親自實現,在這裡寫個總結供自己學習也為大家提供個方便,避免以後大家走彎路,由於我寫的專案比較繁瑣,就不自己寫Demo,直接轉載一篇,只是提醒其中自己遇到的坑

      提示01: 網上都說要觸發onScroll和onFling,必須讓監聽器的onDown的返回值設為true,本人測試false也可,目前沒問題!所以沒有實現翻頁不要糾結再次處!

   提示02: 我載入的是webview頁面,所以這裡必須要注意:一般我們用於接收GestureDetector物件的方法是OnTouchevent();,而在View元件佔用了螢幕空間之後,這個方法就無效了,只有換成 dispatchTouchEvent方法才有效!這個方法貼個程式碼解釋:

@Override

publicboolean dispatchTouchEvent(MotionEvent ev) { //注意這裡不能用ONTOUCHEVENT方法,不然無效的

Toast.makeText(NewsContent.this, "jinru", 1).show();  

detector.onTouchEvent(ev);  

webview.onTouchEvent(ev);//這幾行程式碼也要執行,將webview載入MotionEvent物件一下,這塊載入webview必須加

return super.dispatchTouchEvent(ev);  

}

提示03: 下來就是貼程式碼了,程式碼和網上幾乎相同這個(

提示02的方法必須加到程式碼中!!!!)我是菜鳥不喜勿噴,也歡迎大神指點!

  1. import com.stone.R;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.GestureDetector;  
  5. import android.view.GestureDetector.OnGestureListener;  
  6. import android.view.MotionEvent;  
  7. import android.widget.ViewFlipper;  
  8. //ViewFlipper 和手勢 左右滑動 切換 Activity
  9. publicclass Gesture extends Activity implements OnGestureListener{  
  10.     ViewFlipper flipper; //一次顯示一個子view
  11.     GestureDetector detector; //手勢監測器
  12.     @Override
  13.     protectedvoid onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         detector = new GestureDetector(this);  
  16.         setContentView(R.layout.flipper);  
  17.         flipper = (ViewFlipper) findViewById(R.id.vf_flipper);  
  18.     }  
  19.     @Override
  20.     publicboolean onTouchEvent(MotionEvent event) {  
  21.         returnthis.detector.onTouchEvent(event);//由檢測器 執行 activity.<span style="font-family: Arial, Helvetica, sans-serif;">onTouchEvent</span>
  22.     }  
  23.     /*以下為 OnGestureListener 的方法*/
  24.     @Override
  25.     publicboolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  26.             float velocityY) {  
  27.         if (e1.getX() - e2.getX() > 120) {//向左滑,右邊顯示
  28.             //this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
  29.             //this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
  30.             this.flipper.showNext();   
  31.         }  
  32.         if (e1.getX() - e2.getX() < -120) {//向右滑,左邊顯示
  33.             this.flipper.showPrevious();   
  34.         }  
  35.         returnfalse;  
  36.     }  
  37.     @Override
  38.     publicboolean onDown(MotionEvent e) {  
  39.         returnfalse;  
  40.     }  
  41.     @Override
  42.     publicvoid onShowPress(MotionEvent e) {  
  43.     }  
  44.     @Override//單擊
  45.     publicboolean onSingleTapUp(MotionEvent e) {  
  46.         returnfalse;  
  47.     }  
  48.     @Override
  49.     publicboolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  50.         returnfalse;  
  51.     }  
  52.     @Override// 長按
  53.     publicvoid onLongPress(MotionEvent e) {  
  54.     }  
  55. }  

flipper.xml

[html] view plain copy  print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <ViewFlipper
  7.         android:id="@+id/vf_flipper"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="fill_parent">
  10.         <ImageView
  11.             android:layout_width="fill_parent"
  12.             android:layout_height="fill_parent"
  13.             android:src="@drawable/a2"/>
  14.         <ImageView
  15.             android:layout_width="fill_parent"
  16.             android:layout_height="fill_parent"
  17.             android:src="@drawable/a4"/>
  18.         <ImageView
  19.             android:layout_width="fill_parent"
  20.             android:layout_height="fill_parent"
  21.             android:src="@drawable/a3"/>
  22.     </ViewFlipper>
  23. </LinearLayout>