更新时间:2022-12-05 16:47:50
是否要检测滑动的方向变化,有一种简单的方法:
Should you want to detect the direction change of the swipe there is a simple way:
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.myView).setOnTouchListener(this);
gestureDetector = new GestureDetector(this, this);
}
您可以像这样实现OnTouch和GestureListener:
And you implement OnTouch and GestureListeners like this:
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (distanceY > 0){
// you are going up
} else {
// you are going down
}
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
//the rest of the methods you must implement...