我有 SwipeActivity。当我从右向左滑动时,我想进行活动 B,而当从左向右滑动时,我想进行活动 C。但是我的代码不起作用。请帮我。
我的 SwipeActivity 是:
公共类 SwipeActivity 扩展 Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
View mainview = (View) findViewById(R.id.mainView);
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
Intent in = new Intent(SwipeActivity.this,page1Activity.class);
startActivity(in);
}
public void onSwipeLeft() {
Intent in = new Intent(SwipeActivity.this,page2Activity.class);
startActivity(in);
}
}