Android中三种超实用的滑屏方式汇总（ViewPager、ViewFlipper、ViewFlow）
【移动开发】Android中三种超实用的滑屏方式汇总（ViewPager、ViewFlipper、ViewFlow）

1.ViewPager 2.ViewFlipper 3.ViewFlow

1.2 使用：

activity_main.xml

 1 2 3 4 5 6 7 8 9 10 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              <android.support.v4.view.ViewPager         android:id="@+id/viewpager"         android:layout_width="fill_parent"         android:layout_height="fill_parent" /> 

MainActivity类：

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package comzhf.android_viewpager; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /**  * 主界面：ViewPagerViewPager不在android sdk 自带jar包中，来源google 的补充组件android-support-v4.jar  */ public class ViewPagerActivity extends Activity {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            private ViewPager mViewPager;     List viewList;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    LayoutInflater mInflater = getLayoutInflater().from(this);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    View v1 = mInflater.inflate(R.layout.layout1, null);         View v2 = mInflater.inflate(R.layout.layout2, null);         View v3 = mInflater.inflate(R.layout.layout3, null);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    //添加页面数据         viewList = new ArrayList();         viewList.add(v1);         viewList.add(v2);         viewList.add(v3);         //实例化适配器         mViewPager = (ViewPager) findViewById(R.id.viewpager);         mViewPager.setAdapter(new MyPagerAdapter(viewList));         mViewPager.setCurrentItem(0); //设置默认当前页                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    View view = viewList.get(0);         TextView textView = (TextView) view.findViewById(R.id.text_1);         textView.setText("我是第一页");         Button button = (Button) view.findViewById(R.id.button_1);         button.setOnClickListener(new OnClickListener() {             public void onClick(View v) {                 // TODO Auto-generated method stub                 Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show();                 }         });     } }

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:background="@drawable/guide01" >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  <TextView         android:id="@+id/text_1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:text="叶片一"         android:textSize="25sp" />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  <Button         android:id="@+id/button_1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="点击我"         >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

1.这里我们用了Layout作为每个page的填充数据，其实官方文档说ViewPager+Fragment配合使用更好

2.每个页面的响应事件我们可以在OnPageChangeListener监听器类中进行捕获和处理对应事件。

2.1 介绍：ViewFilpper控件是系统自带控件之一，主要用于在同一个屏幕间的切换及设置动画效果、间隔时间，且可以自动播放。

2.2 使用：

2.2.1 静态加载

activity_main.xml:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >     <ViewFlipper         android:id="@+id/body_flipper"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="#f0f0f0" >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <include             android:id="@+id/layout01"             layout="@layout/page1" />         <include             android:id="@+id/layout02"             layout="@layout/page2" />         <include             android:id="@+id/layout02"             layout="@layout/page3" />         <include             android:id="@+id/layout02"             layout="@layout/page4" />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

MainActivity类：

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package com.zhf.android_viewflipper_view; import android.os.Bundle; import android.app.Activity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.animation.AnimationUtils; import android.widget.ViewFlipper; /**  * ViewFlipper 静态加载  * @author ZHF **/ public class MainActivity extends Activity implements OnTouchListener{     private ViewFlipper viewFlipper;     private float touchDownX;  // 手指按下的X坐标     private float touchUpX;  //手指松开的X坐标                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               viewFlipper = (ViewFlipper) findViewById(R.id.body_flipper);         viewFlipper.setOnTouchListener(this);     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           @Override     public boolean onTouch(View v, MotionEvent event) {         if (event.getAction() == MotionEvent.ACTION_DOWN) {             // 取得左右滑动时手指按下的X坐标             touchDownX = event.getX();             return true;         } else if (event.getAction() == MotionEvent.ACTION_UP) {             // 取得左右滑动时手指松开的X坐标             touchUpX = event.getX();             // 从左往右，看前一个View             if (touchUpX - touchDownX > 100) {                 // 显示上一屏动画                 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,                        R.anim.push_right_in));                 viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,                         R.anim.push_right_out));                 // 显示上一屏的View                 viewFlipper.showPrevious();                 // 从右往左，看后一个View             } else if (touchDownX - touchUpX > 100) {                 //显示下一屏的动画                 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,                         R.anim.push_left_in));                 viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,                         R.anim.push_left_out));                 // 显示下一屏的View                 viewFlipper.showNext();             }             return true;         }         return false;     } }

push_left_in.xml:

 1 2 3 4 5 6 7 8 9 10 11  <set xmlns:android="http://schemas.android.com/apk/res/android" >           

push_left_out.xml:

 1 2 3 4 5 6 7 8 9 10 11  <set xmlns:android="http://schemas.android.com/apk/res/android" >           

2.2.2 动态加载重要

activity_main2.xml

 1 2 3 4 5 6 7 8 9 10 11 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >     <com.zhf.android_viewflipper_view.MyViewFlipper         android:id="@+id/body_flipper"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="#f0f0f0" >      

flipper_view.xml：

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

MyGestureListener类：自定义滑动事件监听器

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package com.zhf.android_viewflipper_view; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; /**  * 自定义滑动事件监听器  * SimpleOnGestureListener已经实现了OnGestureListener接口和OnDoubleTapListener接口，  * 可以有选择性的复写需要的方法，提供方法onFling()作为滑动事件的回调函数  * @author ZHF  *  */ public class MyGestureListener extends SimpleOnGestureListener{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       private OnFlingListener mOnFlingListener;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /**用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发 **/     @Override     public final boolean onFling(final MotionEvent e1, final MotionEvent e2,             final float speedX, final float speedY) {         if (mOnFlingListener == null) {             return super.onFling(e1, e2, speedX, speedY);         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         float XFrom = e1.getX();  //按下坐标         float XTo = e2.getX();         float YFrom = e1.getY();         float YTo = e2.getY();         // 左右滑动的X轴幅度大于100，并且X轴方向的速度大于100         if (Math.abs(XFrom - XTo) > 100.0f && Math.abs(speedX) > 100.0f) {             // X轴幅度大于Y轴的幅度             if (Math.abs(XFrom - XTo) >= Math.abs(YFrom - YTo)) {                 if (XFrom > XTo) {                     // 下一个                     mOnFlingListener.flingToNext();                 } else {                     // 上一个                     mOnFlingListener.flingToPrevious();                 }             }         } else {             return false;         }         return true;