Android - 滑动翻转卡片
//添加正面卡片的内容
//添加反面卡片的内容
2.在 Activity 或 Fragment 中使用 GestureDetector 实现滑动翻转卡片。
private GestureDetectorCompat mDetector;
private final float MIN_X_DIST = 200;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final LinearLayout front = findViewById(R.id.card_front);
final LinearLayout back = findViewById(R.id.card_back);
mDetector = new GestureDetectorCompat(this, new MyGestureListener(front, back));
}
@Override public boolean onTouchEvent(MotionEvent event) { return mDetector.onTouchEvent(event); }
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
View front;
View back;
public MyGestureListener(View front, View back) {
this.front = front;
this.back = back;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY)
&& Math.abs(distanceX) > MIN_X_DIST) {
if (distanceX > 0) {
//从左向右滑动,显示反面
flipCard(true);
} else {
//