Android中手勢滑動翻頁之GestureDetector總結
阿新 • • 發佈:2019-01-06
寫作緣由:最近在做左右滑動翻頁效果,程式碼寫出來經過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的方法必須加到程式碼中!!!!)我是菜鳥不喜勿噴,也歡迎大神指點!
- import com.stone.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.GestureDetector;
- import android.view.GestureDetector.OnGestureListener;
- import android.view.MotionEvent;
- import android.widget.ViewFlipper;
-
//ViewFlipper 和手勢 左右滑動 切換 Activity
- publicclass Gesture extends Activity implements OnGestureListener{
- ViewFlipper flipper; //一次顯示一個子view
- GestureDetector detector; //手勢監測器
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- detector = new GestureDetector(this);
- setContentView(R.layout.flipper);
- flipper = (ViewFlipper) findViewById(R.id.vf_flipper);
- }
- @Override
- publicboolean onTouchEvent(MotionEvent event) {
- returnthis.detector.onTouchEvent(event);//由檢測器 執行 activity.<span style="font-family: Arial, Helvetica, sans-serif;">onTouchEvent</span>
- }
- /*以下為 OnGestureListener 的方法*/
- @Override
- publicboolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- if (e1.getX() - e2.getX() > 120) {//向左滑,右邊顯示
- //this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
- //this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
- this.flipper.showNext();
- }
- if (e1.getX() - e2.getX() < -120) {//向右滑,左邊顯示
- this.flipper.showPrevious();
- }
- returnfalse;
- }
- @Override
- publicboolean onDown(MotionEvent e) {
- returnfalse;
- }
- @Override
- publicvoid onShowPress(MotionEvent e) {
- }
- @Override//單擊
- publicboolean onSingleTapUp(MotionEvent e) {
- returnfalse;
- }
- @Override
- publicboolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- returnfalse;
- }
- @Override// 長按
- publicvoid onLongPress(MotionEvent e) {
- }
- }
flipper.xml
[html] view plain copy print?- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <ViewFlipper
- android:id="@+id/vf_flipper"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:src="@drawable/a2"/>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:src="@drawable/a4"/>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:src="@drawable/a3"/>
- </ViewFlipper>
- </LinearLayout>