在 Activity 判斷滑動手勢主要是透過 View.OnTouchListener 和 GestureDetector.OnGestureListener
透過讓 Activity 繼承 GestureDetector.OnGestureListener 並實作其中的 onFling 方法,在 onFling 方法中判斷第1個按下點的 X 座標和第2個按下點的 X 座標是否超過標準值來決定是否為滑動手勢。
經過測試即使是嵌入在 Activity 的 ListView 也可以成功偵測滑動手勢。
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 ConstraintLayout 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"?>
<android.support.constraint.ConstraintLayout
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"
.../>
</android.support.constraint.ConstraintLayout>
重點為 ConstraintLayout 建立 id,以便在程式碼中參考。
以上便是讓 Activity 加入滑動手勢的判斷,若是想讓該 Activity中
的 ListView 也可偵測滑動,作法相當簡單,直接讓該 ListView 呼叫 setOnTouchListener 和 setLongClickable 即可。
mPatrolPointListView = getListView();
mPatrolPointListViewAdapter =
new PatrolPointListViewAdapter(this, mPatrolPointListPresenter.getPatrolPointItemLayout(firstPage));
setListAdapter(mPatrolPointListViewAdapter);
...
mPatrolPointListView.setOnTouchListener(this);
mPatrolPointListView.setLongClickable(true);
Orignal From: 在Activity中判斷滑動手勢
1 意見:
The best slot machines - JTM Hub
It's 세종특별자치 출장안마 a fun 서산 출장마사지 place to meet the 순천 출장안마 most talented people and get the 이천 출장샵 chance 여주 출장마사지 to play a game that's both fun and exciting. It's easy to
張貼留言