看看android.widget.ViewFlipper 它不包括刷卡功能,但你可以繼承你的自定義類的形式ViewFlipper像下面,並添加你的水平的觀點吧:
public class Fipper extends ViewFlipper {
private final static int SWIPE_MIN_DISTANCE = 80;
private final static int SWIPE_THRESHOLD_VELOCITY = 150;
public Flipper(Context context) {
super(context);
}
public Flipper(Context context, AttributeSet attrs) {
super(context, attrs);
final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
}
@Override
public void showPrevious() {
if (getDisplayedChild() <= 0)
return;
setInAnimation(getContext(), R.anim.slide_in_left);
setOutAnimation(getContext(), R.anim.slide_out_right);
super.showPrevious();
}
@Override
public void showNext() {
if (getDisplayedChild() >= getChildCount() - 1)
return;
setInAnimation(getContext(), R.anim.slide_in_right);
setOutAnimation(getContext(), R.anim.slide_out_left);
super.showNext();
}
private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
showNext();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
showPrevious();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
看看使用帶有片段的'ViewPager'來進行佈局。 – dharms