首页 > 其他分享 >Android 使用ViewFlipper实现左右手势切换页面的demo

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

时间:2023-02-06 17:34:04浏览次数:45  
标签:MotionEvent layout demo return ViewFlipper import Android public android


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

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper android:layout_height="wrap_content"
android:id="@+id/viewFlipper1" android:layout_width="fill_parent"
>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="第一屏" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="第二屏" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>



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;

/**
*
* <p>
* Title: TestViewFlipper.java
* </p>
* <p>
* E-Mail: [email protected]
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-9-29
* </p>
*
* @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;
}

}

标签:MotionEvent,layout,demo,return,ViewFlipper,import,Android,public,android
From: https://blog.51cto.com/u_15955464/6039979

相关文章