Android 使用ViewFlipper实现左右手势切换页面的demo

废话少说,直接上代码吧
main.xml:


android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/viewFlipper1" android:layout_width="fill_parent"
>
android:layout_height="wrap_content">
android:layout_height="wrap_content" android:text="第一屏" />

android:layout_height="wrap_content">
android:layout_height="wrap_content" android:text="第二屏" />





TestViewFlipper.java:

package com.iaiai.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

/**
*
*


* Title: TestViewFlipper.java
*


*


* E-Mail: [email protected]
*


*


* QQ: 176291935
*


*


* Http: iaiai.iteye.com
*


*


* Create time: 2011-9-29
*


*
* @author 丸子
* @version 0.0.1
*/
public class TestViewFlipper extends Activity implements OnGestureListener {

private static final String TAG = "TestViewFlipper";
private ViewFlipper viewFlipper;
private GestureDetector detector;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v(TAG, "touched");
return this.detector.onTouchEvent(event);
}

public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i(TAG, "fling...");
if (e1.getX() > e2.getX()) {
viewFlipper.showNext();
} else if (e1.getX() < e2.getX()) {
viewFlipper.showPrevious();
} else {
return false;
}
return true;
}

}

你可能感兴趣的:(android基础知识)