</pre><p>主要代码如下:</p><p><pre name="code" class="html">public class SnsConstant {
private static final int FLING_MIN_DISTANCE = 50;
private static final int FLING_MIN_VELOCITY = 0;
public static int getFlingMinDistance() {
return FLING_MIN_DISTANCE;
}
public static int getFlingMinVelocity() {
return FLING_MIN_VELOCITY;
}
}
public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {
GestureDetector mGestureDetector;
SnsConstant snsConstant;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector((OnGestureListener) this);
RelativeLayout viewSnsLayout = (RelativeLayout)findViewById(R.id.viewSnsLayout);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if (e1.getX()-e2.getX() > snsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
// 切换Activity
Intent intent = new Intent(MainActivity.this, UpdateActivity.class);
startActivity(intent);
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} /*else if (e2.getX()-e1.getX() > snsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
// 切换Activity
Intent intent = new Intent(MainActivity.this, UpdateActivity.class);
startActivity(intent);
Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
} */
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(arg1);
}
}