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

简介:



现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么。。。j_0059.gif嘿嘿),由于前段时间项目的需要,所以也对其研究了一下,总的来说滑屏实现有三种方式:(至于其他的实现方式目前后还没碰到。。。)

1.ViewPager 2.ViewFlipper 3.ViewFlow


一.ViewPager


官方文档介绍:http://developer.android.com/reference/android/support/v4/view/ViewPager.html

175956838.png


根据继承关系我们可以看出,ViewPager不在android sdk 自带jar包中,来源google 的补充组件android-support-v4.jar中,所以我们在3.0以前的版本中使用就需要导入该jar包了。


1.1 介绍该类是一个布局管理器,它允许用户通过滑动左、右页的数据。你必须要一个实现了PagerAdapter接口从而生成的页面视图。


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"  />
</ RelativeLayout >


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<View> 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<View>();
         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();
                 }
         });
     }
}


这里还有三个布局文件:layout1.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
<? xml  version = "1.0"  encoding = "utf-8" ?>
< 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 = "点击我"
         >
     </ Button >
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
</ LinearLayout >


效果图:

183843192.png 183843821.png 183845132.png

补充说明:

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

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


二.ViewFlipper


官方文档:http://developer.android.com/reference/android/widget/ViewFlipper.html

190300918.png

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

顺便提及一下,View动画关系图:

195450865.png

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"  />
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
     </ ViewFlipper >
</ RelativeLayout >

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
<?xml version= "1.0"  encoding= "utf-8" ?>
< set  xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <translate
         android:duration= "500"
         android:fromXDelta= "100.0%p"
         android:toXDelta= "0.0"  />
     <alpha
         android:duration= "500"
         android:fromAlpha= "0.1"
         android:toAlpha= "1.0"  />
</ set >

push_left_out.xml:

1
2
3
4
5
6
7
8
9
10
11
<?xml version= "1.0"  encoding= "utf-8" ?>
< set  xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <translate
         android:duration= "500"
         android:fromXDelta= "0.0"
         android:toXDelta= "-100.0%p"  />
     <alpha
         android:duration= "500"
         android:fromAlpha= "1.0"
         android:toAlpha= "0.1"  />
</ set >

效果:

204143396.png 204144556.png

向左滑屏 向右滑屏

补充

上述的page只有4个,而真实项目中的page页面个数是不确定的,所以下面这种方式是项目中经常用到的。


2.2.2 动态加载重要

参考文章:http://blog.csdn.net/yuzhiboyi/article/details/7702953

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"  >
     </ com.zhf.android_viewflipper_view.MyViewFlipper >
</ RelativeLayout >

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
<?xml version= "1.0"  encoding= "utf-8" ?>
<ScrollView xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:scrollbars= "none"  >
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     <LinearLayout
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:gravity= "center"
         android:orientation= "vertical"  >
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
         <ImageView
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@drawable/ic_launcher"  />
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
         <TextView
             android:id= "@+id/textView"
             android:textSize= "100dip"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"  />
     </LinearLayout>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
</ScrollView>

注:这里并不是所有的View都能有onFling回调函数,外部需要加ScrollView !

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 ;
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
     /**自定义滑动的回调接口**/
     public  interface  OnFlingListener {
         void  flingToNext();   //滑动到下一页
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
         void  flingToPrevious();   //滑动到上一页
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     public  OnFlingListener getOnFlingListener() {
         return  mOnFlingListener;
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
     public  void  setOnFlingListener(OnFlingListener mOnFlingListener) {
         this .mOnFlingListener = mOnFlingListener;
     }
}


MyViewFlipper类:自定义View滑动类:监听滑动事件,并做切换视图的处理。

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package  com.zhf.android_viewflipper_view;
import  android.content.Context;
import  android.util.AttributeSet;
import  android.view.GestureDetector;
import  android.view.MotionEvent;
import  android.view.View;
import  android.widget.ViewFlipper;
import  com.zhf.android_viewflipper_view.MyGestureListener.OnFlingListener;
/**
  * 自定义View滑动类:监听滑动事件,并做切换视图的处理。
  * @author ZHF
  *
  */
public  class  MyViewFlipper  extends  ViewFlipper  implements  OnFlingListener {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
     //手势监听类
     private  GestureDetector mGestureDetector =  null ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     private  OnViewFlipperListener mOnViewFlipperListener =  null ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     public  MyViewFlipper(Context context) {
         super (context);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     public  MyViewFlipper(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     public  void  setOnViewFlipperListener(OnViewFlipperListener mOnViewFlipperListener) {
         this .mOnViewFlipperListener = mOnViewFlipperListener;
         //初始化自定义滑动事件监听器
         MyGestureListener myGestureListener =  new  MyGestureListener();
         //绑定自定义的滑动监听器
         myGestureListener.setOnFlingListener( this );
         mGestureDetector =  new  GestureDetector(myGestureListener);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     @Override
     public  boolean onInterceptTouchEvent(MotionEvent ev) {
         if  ( null  != mGestureDetector) {
             return  mGestureDetector.onTouchEvent(ev);
         else  {
             return  super .onInterceptTouchEvent(ev);
         }
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     /**向下一条滑动事件**/
     @Override
     public  void  flingToNext() {
         if  ( null  != mOnViewFlipperListener) {
             int  childCnt = getChildCount();
             if  (childCnt ==  2 ) {
                 removeViewAt( 1 );
             }
             addView(mOnViewFlipperListener.getNextView(),  0 );
             if  ( 0  != childCnt) {
                 setInAnimation(getContext(), R.anim.push_left_in);
                 setOutAnimation(getContext(), R.anim.push_left_out);
                 setDisplayedChild( 0 );
             }
         }
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     /**向上一条滑动事件**/
     @Override
     public  void  flingToPrevious() {
         if  ( null  != mOnViewFlipperListener) {
             int  childCnt = getChildCount();
             if  (childCnt ==  2 ) {
                 removeViewAt( 1 );
             }
             addView(mOnViewFlipperListener.getPreviousView(),  0 );
             if  ( 0  != childCnt) {
                 setInAnimation(getContext(), R.anim.push_right_in);
                 setOutAnimation(getContext(), R.anim.push_right_out);
                 setDisplayedChild( 0 );
             }
         }
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
     /**自定义View变化监听回调接口**/
     public  interface  OnViewFlipperListener {
         View getNextView();    //获取下一页View
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
         View getPreviousView();   //获取上一页View
     }
}

MainActivity2类:

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
package  com.zhf.android_viewflipper_view;
import  com.zhf.android_viewflipper_view.MyViewFlipper.OnViewFlipperListener;
import  android.app.Activity;
import  android.os.Bundle;
import  android.view.LayoutInflater;
import  android.view.View;
import  android.widget.ScrollView;
import  android.widget.TextView;
/**
  * ViewFlipper 动态加载
  * @author ZHF
  */
public  class  MainActivity2  extends  Activity  implements  OnViewFlipperListener{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
       private  MyViewFlipper myViewFlipper;
       private  int  currentNumber;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
         currentNumber =  1 ;   //默认页号
         myViewFlipper = (MyViewFlipper) findViewById(R.id.body_flipper);
         //给ViewFlipper绑定自定义的滑动监听器
         myViewFlipper.setOnViewFlipperListener( this );
         //初始化页面数据,即View
         myViewFlipper.addView(createView(currentNumber));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
     }
     /**获取下一页View**/
     @Override
     public  View getNextView() {
         currentNumber = currentNumber ==  10  1  : currentNumber +  1 ;
         return  createView(currentNumber);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
     /**获取上一页View**/
     @Override
     public  View getPreviousView() {
         currentNumber = currentNumber ==  1  10  : currentNumber -  1 ;
         return  createView(currentNumber);
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
     /**更换View数据:这里是根据页号来更换textView上的文字**/
     private  View createView( int  currentNumber) {
         LayoutInflater layoutInflater = LayoutInflater.from( this );
         ScrollView resultView = (ScrollView) layoutInflater.inflate(R.layout.flipper_view,  null );
         ((TextView) resultView.findViewById(R.id.textView)).setText(currentNumber +  "" );
         return  resultView;
     }
}

效果图:

132603593.png--向左滑动(渐变过程不好截图)--> 132604221.png

补充说明:

上述的三个类:

MyGestureListener:继承了SimpleGestureListener手势监听类, 复写了该类onFling()方法,用于监听用户按下滑动事件的处理;还自定义了滑动的回调接口OnFlingListener(包含了两个抽象方法flingToNext(),flingToPrevious)。


MyViewFlipper:是一个自定义ViewFlipper,该类首先实现和绑定了上一个类中的滑动的回调接口OnFlingListener,完成了接口中两个重要的方法。同时定义了一个View变化监听回调接口OnViewFlipperListener(包含了两个抽象方法getNextView(),getPreviousView())。


MainActivity2:加载布局,实现监听,统一处理页面数据View和滑动事件的绑定。


三.ViewFlow类


3.1介绍

ViewFlow不是google官方的api,它是gethub上的一个开源项目,利用ViewFlow可以产生视图切换的效果。ViewFlow 相当于 Android UI 部件提供水平滚动的 ViewGroup,使用 Adapter 进行条目绑定,例如ViewPager或是ViewFlipper。它提供了三个组件ViewFlow、FlowIndicator和TitleFlowIndicator,一般情况下,当你需要做一个滑动然而不确定view的数目时,可以考虑使用ViewFlow。如果你的view数目确定,使用Fragments 或兼容库里的ViewPager比较好 。


3.2使用:

A.首先下载ViewFlow开源库代码:

官方文档:https://github.com/pakerfeldt/android-viewflow

201334458.png

B.下载之后我们解压打开viewflow文件夹:

201922983.png

C.建项目将这三个类直接复制过来放项目中使用即可。

202404410.png







注:这里可能还需要一个styleable文件,直接将value文件夹下的attrs.xml拷入即可。

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
26
27
28
29
30
31
32
33
34
35
36
37
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:app = "http://schemas.android.com/apk/res/com.zhf.android_viewflow"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:orientation = "vertical"  >
     < FrameLayout
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent"
         android:orientation = "vertical"  >
         < RelativeLayout
             android:layout_width = "fill_parent"
             android:layout_height = "135dp"
             android:orientation = "vertical"  >
                                                                                                                                                                                                                                                                                                 
             < com.zhf.android_viewflow.ViewFlow
                 android:id = "@+id/viewflow"
                 android:layout_width = "fill_parent"
                 android:layout_height = "fill_parent"
                 app:sidebuffer = "3" />
         </ RelativeLayout >
         < com.zhf.android_viewflow.CircleFlowIndicator
             android:id = "@+id/viewflowindic"
             android:layout_width = "wrap_content"
             android:layout_height = "140dp"
             android:layout_alignParentBottom = "true"
             android:layout_gravity = "center_horizontal"
             app:fadeOut = "0"
             app:inactiveType = "fill"
             android:paddingTop = "125dp"  />
         <!--圆点指示器还支持activeColor、inactiveColor、activeType(填充或描边)、
             inactiveType(填充或描边)、
             fadeOut(设置圆点自动隐藏的秒数,若为0则不会自动隐藏)、
             radius(圆点的半径)等。
         -->
     </ FrameLayout >
</ LinearLayout >

注:这里需要强调一下,因为使用第三方的库组件,所以要在使用之前引入:

xmlns:app="http://schemas.android.com/apk/res/com.zhf.android_viewflow"


main_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:isScrollContainer = "true"
     android:scrollbarAlwaysDrawVerticalTrack = "true"
     android:scrollbars = "vertical"  >
<!-- isScrollContainer   设置当前View为滚动容器
     scrollbarAlwaysDrawVerticalTrack      设置是否始终显示垂直滚动条-->
     < ImageView
         android:id = "@+id/imgView"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:layout_gravity = "center_vertical|center_horizontal"  >
     </ ImageView >
</ LinearLayout >

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
package  com.zhf.android_viewflow;
import  android.os.Bundle;
import  android.app.Activity;
import  android.content.res.Configuration;
public  class  MainActivity  extends  Activity {
     private  ViewFlow viewFlow;
     private  CircleFlowIndicator indic;   //页表指示器
                                                                                                                                                                                                              
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
                                                                                                                                                                                                                  
         viewFlow = (ViewFlow) findViewById(R.id.viewflow);
         //为其绑定适配器
         viewFlow.setAdapter( new  ImageAdapter( this ), 5 );  //初始位置5
                                                                                                                                                                                                              
         indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
         //为viewFlow绑定页表指示器
         viewFlow.setFlowIndicator(indic);
     }
                                                                                                                                                                                                              
     /**处理转屏操作**/
     @Override
     public  void  onConfigurationChanged(Configuration newConfig) {
         super .onConfigurationChanged(newConfig);
         viewFlow.onConfigurationChanged(newConfig);
     }
}


这里还有一个图片适配器:ImageAdapter

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
package  com.zhf.android_viewflow;
import  com.cjf.ui.R;
import  android.content.Context;
import  android.view.LayoutInflater;
import  android.view.View;
import  android.view.ViewGroup;
import  android.widget.BaseAdapter;
import  android.widget.ImageView;
public  class  ImageAdapter  extends  BaseAdapter{
                                                                                                                                                                              
     private  LayoutInflater mInflater;
     //图片资源的id
     private  static  final  int [] ids = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
         R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h};
     public  ImageAdapter(Context context) {
         this .mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
     @Override
     public  int  getCount() {
         // TODO Auto-generated method stub
         return  ids ==  null  0  :ids.length;
     }
     @Override
     public  Object  getItem( int  position) {
         // TODO Auto-generated method stub
         return  position;
     }
     @Override
     public  long getItemId( int  position) {
         // TODO Auto-generated method stub
         return  position;
     }
     @Override
     public  View getView( int  position, View convertView, ViewGroup parent) {
                                                                                                                                                                                  
         if  (convertView ==  null ) {
             convertView = mInflater.inflate(R.layout.image_item,  null );
         }
         ((ImageView) convertView.findViewById(R.id.imgView)).setImageResource(ids[position]);
         return  convertView;
     }
}


运行一下吧!效果图:

222917441.jpg转屏后 222954776.jpg


恩,终于写完了! 希望这篇博客能帮助到大家!好累~~j_0068.gif

三个例子的源码我已总结好(独立的三个项目,压缩在一个zip里了)

下载地址:http://down.51cto.com/data/976370





     本文转自zhf651555765 51CTO博客,原文链接:http://blog.51cto.com/smallwoniu/1308959,如需转载请自行联系原作者


相关文章
|
6月前
|
XML Java Android开发
Android Studio App开发之翻页视图ViewPager的讲解及实战(附源码 包括翻页视图和翻页标签栏)
Android Studio App开发之翻页视图ViewPager的讲解及实战(附源码 包括翻页视图和翻页标签栏)
895 0
|
存储 传感器 定位技术
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
188 0
|
1月前
|
Java Linux Android开发
移动应用开发与操作系统的交互:深入理解Android和iOS
在数字时代,移动应用成为我们日常生活的一部分。本文将深入探讨移动应用开发的核心概念、移动操作系统的工作原理以及它们如何相互作用。我们将通过实际代码示例,展示如何在Android和iOS平台上创建一个简单的“Hello World”应用,并解释其背后的技术原理。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的见解和知识。
|
5月前
|
Android开发 容器
35. 【Android教程】视频页面:ViewPager
35. 【Android教程】视频页面:ViewPager
60 3
|
6月前
|
缓存 算法 网络协议
Android面试回忆录移动应用开发专业核心课程
Android面试回忆录移动应用开发专业核心课程
|
2月前
|
存储 Java 开发工具
移动应用开发之旅:探索Android操作系统的无限可能
【8月更文挑战第56天】随着智能手机的普及,移动应用已成为我们日常生活中不可或缺的一部分。本文将引导读者了解移动应用开发的基本概念,重点探讨Android操作系统的开发环境搭建、界面设计、功能实现以及与后端服务的交互。通过简单的代码示例和清晰的步骤说明,即便是初学者也能快速入门,开启自己的移动应用开发之旅。
|
2月前
|
设计模式 前端开发 JavaScript
探索移动应用开发:从Android到iOS的跨平台之旅
【9月更文挑战第21天】在这篇文章中,我们将一同揭开移动应用开发的神秘面纱,从Android和iOS这两个主流平台出发,探讨如何利用现代技术栈实现跨平台开发。文章将通过具体的代码示例,带领读者理解不同平台间的差异与联系,以及如何运用React Native框架简化开发流程,实现一次编写,多平台运行的目标。无论你是刚入门的新手还是希望拓展技能的老手,这篇文章都将为你提供宝贵的知识和启示。
70 3
|
1月前
|
开发工具 Android开发 iOS开发
移动应用开发的艺术:探索Android与iOS的操作系统特性
【9月更文挑战第33天】在数字时代的浪潮中,移动应用已成为我们日常生活不可或缺的一部分。本文将深入探讨两个主流移动操作系统——Android和iOS——的独特特性,并分析它们如何影响移动应用的开发过程。我们将通过比较这两个系统的设计哲学、用户界面(UI)设计、开发工具以及市场策略,来揭示开发者如何在这些不同的平台上打造出色的用户体验。无论你是开发者还是对移动技术感兴趣的读者,这篇文章都将为你提供宝贵的见解。
|
3月前
|
机器学习/深度学习 Android开发 iOS开发
探索移动应用开发的未来:Android与iOS的较量
【8月更文挑战第21天】在数字时代的浪潮中,移动应用已成为日常生活不可或缺的一部分。两大主流平台,Android和iOS,各自以其独特的优势和挑战塑造着开发者和用户的世界。本文将深入探讨这两个平台的发展动态、设计理念及其对移动应用开发未来的影响。
|
3月前
|
Android开发
Android使用ViewPager做无限轮播,人为滑动时停止
Android使用ViewPager做无限轮播,人为滑动时停止
80 2
下一篇
无影云桌面