public
class
Flip
extends
Activityimplements OnGestureListener {
private
GestureDetector detector;
private
ViewFlipper flipper;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)
this
.findViewById(R.id.ViewFlipper01);
flipper.addView(addButtonByText(
"按钮"
),newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
detector =
new
GestureDetector(
this
);
}
public
View addButtonByText(String text){
Button btn =
new
Button(
this
);
btn.setText(text);
return
btn;
}
public
View addTextByText(String text){
TextView tv =
new
TextView(
this
);
tv.setText(text);
tv.setGravity(
1
);
return
tv;
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
Log.i(
"Fling"
,
"Activity onTouchEvent!"
);
return
this
.detector.onTouchEvent(event);
}
@Override
public
boolean
onDown(MotionEvent e) {
return
false
;
}
@Override
public
boolean
onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,
float
velocityY) {
Log.i(
"Fling"
,
"Fling Happened!"
);
if
(e1.getX() - e2.getX() >
120
) {
this
.flipper.setInAnimation(AnimationUtils.loadAnimation(
this
,R.anim.push_left_in));
this
.flipper.setOutAnimation(AnimationUtils.loadAnimation(
this
,R.anim.push_left_out));
this
.flipper.addView(addTextByText(
"文本框"
),newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
this
.flipper.showNext();
return
true
;
}
else
if
(e1.getX() - e2.getX() < -
120
) {
this
.flipper.setInAnimation(AnimationUtils.loadAnimation(
this
,R.anim.push_right_in));
this
.flipper.setOutAnimation(AnimationUtils.loadAnimation(
this
,R.anim.push_right_out));
this
.flipper.showPrevious();
return
true
;
}
return
true
;
}
@Override
public
void
onLongPress(MotionEvent e) {
}
@Override
public
boolean
onScroll(MotionEvent e1, MotionEvent e2, floatdistanceX,
float
distanceY) {
return
false
;
}
@Override
public
void
onShowPress(MotionEvent e) {
}
@Override
public
boolean
onSingleTapUp(MotionEvent e) {
return
false
;
}
}