网上找的,挺好的,忘记网址了,做下备忘吧
public class GestureBackBaseActivity extends Activity implements View.OnTouchListener, GestureDetector.OnGestureListener {
private int verticalMinDistance = 20;
private int minVelocity = 0;
GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout viewSnsLayout = new LinearLayout(this);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
setContentView(viewSnsLayout);
mGestureDetector = new GestureDetector(this, (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 e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {// 切换Activity
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
finish();
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
}