onfling方法,会判断手势从按下到抬起移动的过程,在此方法中进行切换activity等操作。
private int verticalMinDistance = 20;
private int minVelocity = 0;
private GestureDetector mGestureDetector;
rightRelative.setOnTouchListener(this);
mGestureDetector = new GestureDetector(this, this);
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetector.onTouchEvent(motionEvent);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.unzoom_out, R.anim.unzoom_in);
} else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
Toast.makeText(this, "向右", Toast.LENGTH_SHORT).show();
}
return false;
}