在 Activity 判斷滑動手勢主要是透過 View.OnTouchListener 和 GestureDetector.OnGestureListener
透過讓 Activity 繼承 GestureDetector.OnGestureListener 並實作其中的 onFling 方法,在 onFling 方法中判斷第 1 個按下點的 X 座標和第 2 個按下點的 X 座標是否超過標準值來決定是否為滑動手勢。
import android.support.constraint.ConstraintLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class FlingGestureExampleMainActivity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener{ private static final String TAG = FlingGestureExampleMainActivity.class.getSimpleName(); //main ui private LinearLayout mBasicLayout; private GestureDetector mGesetureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fling_gesture_example_main_activity); mBasicLayout = findViewById(R.id.basic_layout_activity); mBasicLayout.setOnTouchListener(this); mBasicLayout.setLongClickable(true); mGesetureDetector = new GestureDetector((GestureDetector.OnGestureListener) this); } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent firstMotion, MotionEvent secondMotion, float velocityX, float velocityY) { final int FLING_MIN_DISTANCE = 100; final int FLING_MIN_VELOCITY = 200; boolean isFlingToLeft = firstMotion.getX() - secondMotion.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY; if (isFlingToLeft) { Log.d(TAG, "to left"); } boolean isFlingToRight = firstMotion.getX() - secondMotion.getX() < FLING_MIN_DISTANCE && Math.abs(velocityX) < FLING_MIN_VELOCITY; if (isFlingToRight) { Log.d(TAG, "to right"); } return false; } @Override public boolean onTouch(View view, MotionEvent motionEvent) { return mGesetureDetector.onTouchEvent(motionEvent); } }
主要的重點為讓 Activity實作 onDown, onShowPress, on SingleTapUp, onScroll, onLongPress, onFling, onTouch 方法,
並在 onFling 方法判斷滑動的邏輯。
以及第23~26行的手勢和 layout 物件的連結
layout檔(fling_gesture_example_main_activity.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/basic_layout_activity" tools:context=".FlingGestureExampleMainActivity"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" .../> </LinearLayout>
重點為 LinearLayout 建立 id,以便在程式碼中參考。
以上便是讓 Activity 加入滑動手勢的判斷,若是想讓該 Activity中
的 ListView 也可偵測滑動,作法相當簡單,直接讓該 ListView 呼叫 setOnTouchListener 和 setLongClickable 即可。
mListView = getListView(); mListViewAdapter = new ListViewAdapter(); setListAdapter(mListViewAdapter); ... mPatrolPointListView.setOnTouchListener(this); mPatrolPointListView.setLongClickable(true);