80,351
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<TextView android:text="My course" ></TextView>
<ViewFlipper>
<LinearLayout>
<TextView android:text="11111"></TextView>
</LinearLayout>
<LinearLayout>
<TextView android:text="22222"></TextView>
</LinearLayout>
<LinearLayout>
<TextView android:text="33333"></TextView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
vf = (ViewFlipper) findViewById(R.id.layoutswitcher);
vf.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent touchevent) {
Log.e("touchevent", String.valueOf(touchevent.getAction()));
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_MOVE:
{
float currentX = touchevent.getX();
Log.e("touchevent", String.valueOf(oldTouchValue)+" - "+String.valueOf(currentX));
if(currentX>oldTouchValue){
vf.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.push_right_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.push_left_out));
vf.showNext();
}
else {
vf.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.push_left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.push_right_out));
vf.showPrevious();
}
break;
}
}
return true;
}
});
public class MyCourse extends Activity implements OnGestureListener {
private GestureDetector detector;
protected void onCreate(Bundle savedInstanceState) {
detector = new GestureDetector(getApplicationContext(),this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() > e2.getX()) {
this.vf.showNext();
} else if (e1.getX() < e2.getX()) {
this.vf.showPrevious();
} else {
return false;
}
return true;
}
}