ViewPager实现引导页

废话就不多说了,直接上效果图和代码

ViewPager实现引导页_微信

ViewPager实现引导页_android_02

fry.Activity01



1 package fry;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.ListIterator;
8
9 import com.example.viewPager.R;
10
11 import android.app.Activity;
12 import android.os.Bundle;
13 import android.support.v4.view.ViewPager;
14 import android.support.v4.view.ViewPager.OnPageChangeListener;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.view.ViewGroup.LayoutParams;
18 import android.widget.ImageView;
19 import android.widget.LinearLayout;
20
21 public class Activity01 extends Activity implements OnPageChangeListener,OnClickListener{
22 private ViewPager viewPager;
23 private List<View> views;//用来存放放进ViewPager里面的ImageView
24 private int[] imageID;//
25 private ImagePagerAdapter adapter;//适配器
26 private LinearLayout linearLayout;
27 private int currentPoint;//当前被选中中页面的下标
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 // TODO Auto-generated method stub
31 super.onCreate(savedInstanceState);
32 setContentView(R.layout.activity01);
33 initData();
34 initView();
35
36 }
37 //初始化view
38 void initView(){
39 adapter=new ImagePagerAdapter(views);
40 viewPager=(ViewPager) findViewById(R.id.viewPager);
41 viewPager.setAdapter(adapter);
42 linearLayout=(LinearLayout) findViewById(R.id.linearLayout);
43 initPoint();
44 viewPager.setOnPageChangeListener(this);
45 }
46
47 //初始化数据
48 void initData(){
49 //将图片的id全部放到imageID集合中
50 imageID=new int[]{R.drawable.guide1,R.drawable.guide2,R.drawable.guide3,R.drawable.guide4};
51 views=new ArrayList<View>();
52 //给iamgeView设置LayoutParams参数
53 LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
54 for(int i:imageID){
55 //不确定1
56 ImageView imageView=new ImageView(this);
57 imageView.setImageResource(i);
58 imageView.setLayoutParams(layoutParams);
59 views.add(imageView);
60 }
61
62
63 }
64
65 /*
66 * 初始化页面下方的点
67 */
68 private void initPoint(){
69 ImageView[] imageView=new ImageView[imageID.length];
70 for(int i=0;i<linearLayout.getChildCount();i++){
71 imageView[i]=(ImageView) linearLayout.getChildAt(i);
72 imageView[i].setImageResource(R.drawable.point_normal);
73 imageView[i].setOnClickListener(this);
74 imageView[i].setTag(i);
75 }
76 currentPoint=0;
77 imageView[currentPoint].setImageResource(R.drawable.point_select);
78 }
79 /*
80 * 将页面下方的点全部置为未选中状态
81 */
82 private ImageView[] clearPoint(){
83 ImageView[] imageView=new ImageView[imageID.length];
84 for(int i=0;i<linearLayout.getChildCount();i++){
85 imageView[i]=(ImageView) linearLayout.getChildAt(i);
86 imageView[i].setImageResource(R.drawable.point_normal);
87 }
88 return imageView;
89 }
90
91 @Override
92 public void onPageScrollStateChanged(int arg0) {
93 // TODO Auto-generated method stub
94
95 }
96
97 @Override
98 public void onPageScrolled(int arg0, float arg1, int arg2) {
99 // TODO Auto-generated method stub
100
101 }
102
103 /*
104 * 页面被选中时候执行的方法
105 * (non-Javadoc)
106 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
107 */
108 @Override
109 public void onPageSelected(int position) {
110 // TODO Auto-generated method stub
111 ImageView[] imageView= clearPoint();
112 imageView[position].setImageResource(R.drawable.point_select);
113 }
114
115 //小圆点点击事件
116 @Override
117 public void onClick(View v) {
118 // TODO Auto-generated method stub
119 int i=(Integer) v.getTag();
120 viewPager.setCurrentItem(i);
121 }
122
123 }


 

fry.ImagePagerAdapter



1 package fry;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.support.v4.view.PagerAdapter;
7 import android.support.v4.view.ViewPager;
8 import android.view.View;
9 import android.view.ViewGroup;
10
11 public class ImagePagerAdapter extends PagerAdapter{
12
13 private List<View> views;
14
15
16
17 public ImagePagerAdapter(List<View> views) {
18 this.views=views;
19 }
20
21 @Override
22 public int getCount() {
23 // TODO Auto-generated method stub
24 return this.views.size();
25 }
26 /**
27 * position是在viewPager中显示图片的位置
28 * 我对应的图片放到对应的位置就好了
29 * instantiateItem和destroyItem是对应的
30 * 一个是创建item,一个是销毁item
31 */
32 @Override
33 public Object instantiateItem(ViewGroup container, int position) {
34 ((ViewPager)container).addView(views.get(position));
35 return views.get(position);
36 }
37
38 @Override
39 public boolean isViewFromObject(View arg0, Object arg1) {
40 // TODO Auto-generated method stub
41 return arg0==arg1;
42 }
43
44 @Override
45 public void destroyItem(ViewGroup container, int position, Object object) {
46 ((ViewPager)container).removeView(views.get(position));
47 }
48
49 }


/viewPager/res/layout/activity01.xml



1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <android.support.v4.view.ViewPager
8 android:id="@+id/viewPager"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 >
12
13 </android.support.v4.view.ViewPager>
14
15 <LinearLayout
16 android:id="@+id/linearLayout"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:orientation="horizontal"
20 android:layout_gravity="bottom|center"
21 >
22 <ImageView
23 android:id="@+id/point1"
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:src="@drawable/point_select"
27 android:padding="25dip"
28 />
29 <ImageView
30 android:id="@+id/point2"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:src="@drawable/point_normal"
34 android:padding="25dip"
35 />
36 <ImageView
37 android:id="@+id/point3"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:src="@drawable/point_normal"
41 android:padding="25dip"
42 />
43 <ImageView
44 android:id="@+id/point4"
45 android:layout_width="wrap_content"
46 android:layout_height="wrap_content"
47 android:src="@drawable/point_normal"
48 android:padding="25dip"
49 />
50
51 </LinearLayout>
52
53 </FrameLayout>


ViewPager实现引导页_微信_03 

 

1、多个控件(这里指小圆点)可以用List来一起访问

2、可用动态的控件设置tag来区分控件