我想在双方放置新活动,即在左侧滑动' activity2 ',右滑动' activity3 '
我尝试了很多次,但它只适用于一方。 即左滑动工作正常,但是没有,如果我复制并更改代码中的一些内容,它会给我 onFling 错误。
我错过了什么?
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.WindowManager;
import android.view.MotionEvent;
public class Account extends AppCompatActivity {
private GestureDetectorCompat gestureObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_account);
MediaPlayer Player = MediaPlayer.create (this, R.raw.wonder);
Player.start();
gestureObject = new GestureDetectorCompat(this, new LearnGesture());
}
@Override
public boolean onTouchEvent (MotionEvent event) {
this.gestureObject.onTouchEvent(event);
return super.onTouchEvent(event);
}
class LearnGesture extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
if (event2.getX() > event1.getX()) {
Intent intent = new Intent(
Account.this, Settings.class);
finish();
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
} else if (event2.getX() < event1.getX()) {
}
return true;
}
public boolean onFling(MotionEvent event1, MotionEvent event3,
float velocityX, float velocityY) {
if (event3.getX() > event1.getX()) {
Intent intent = new Intent(
Account.this, Achievement.class);
finish();
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
} else if (event3.getX() < event1.getX()) {
}
return true;
}
}
}