Android:启动引导页实现
前言
本文引用地址:https://www.eepw.com.cn/article/201609/304562.htm基本上现在所有的应用都会有一个欢迎界面,在欢迎界面对应用做一个整体的介绍,然后在跳入到主界面,这次要说的这个引导页就是带翻页的引导页。效果如下所示
概要实现
主要分为两部分功能,一个是翻页效果,一个是页面位置指示器。为了实现翻页效果我采用系统自带的ViewPager对象来实现;页面指示器则通过一个LinearLayout在其中放置相应个数的图片,然后根据页面的滑动动态修改各个图片的资源。布局文件如下所示
复制代码
1
2 xmlns:tools=http://schemas.android.com/tools
3 android:layout_width=match_parent
4 android:layout_height=match_parent
5 tools:context=.MainActivity >
6
7
8 xmlns:android=http://schemas.android.com/apk/res/android
9 android:id=@+id/welcome_pager
10 android:layout_width=match_parent
11 android:layout_height=match_parent />
12
13
14
15 android:id=@+id/director
16 android:layout_width=match_parent
17 android:layout_height=wrap_content
18 android:gravity=center_horizontal
19 android:orientation=horizontal
20 android:layout_marginBottom=15dip
21 android:layout_alignParentBottom=true
22 >
23
24
25 android:layout_width=wrap_content
26 android:layout_height=wrap_content
27 android:background=@drawable/pageindicator_on />
28
29
30 android:layout_width=wrap_content
31 android:layout_height=wrap_content
32 android:background=@drawable/pageindicator_off />
33
34
35 android:layout_width=wrap_content
36 android:layout_height=wrap_content
37 android:background=@drawable/pageindicator_off />
38
39
40 android:layout_width=wrap_content
41 android:layout_height=wrap_content
42 android:background=@drawable/pageindicator_off />
43
44
45
复制代码
ViewPager
先来看下官方解释:Layout manager that allows the user to flip left and right through pages of data.意思是说,Viewpage是一个允许用户在多个页面数据之间通过左滑或者右滑的方式切换页面数据的布局管理器。
主要功能点有两部分,数据适配器Adapter,和事件监听器OnPageChangeListener。数据适配器用来管理这个ViewPager对象的显示内容,而OnPageChangeListener用来处理当页面切换的时候的行为动作,我修改页面指示器就是通过这个事件来完成的。
适配器
复制代码
1 class pagerAdapter extends FragmentPagerAdapter{
2
3 public pagerAdapter(FragmentManager fm) {
4 super(fm);
5 }
6
7 @Override
8 public Fragment getItem(int arg0) {
9 //得到要显示的对象并初始化图片
10 WelcomeFragment fm = new WelcomeFragment();
11 fm.setImg(imgs.get(arg0));
12
13 return fm;
14 }
15
16 @Override
17 public int getCount() {
18 return imgs.size();
19 }
20
21 }
复制代码
上面这段就是ViewPager要用的适配器了,其中imgs是一个id数组,存放了要在欢迎界面展示的图片的id,WelcomeFragment是一个Fragment类,用来展示页面内容,这两个代码会在完整代码中体现。两个方法需要实现,getCout,用来表示有多少个页面;getItem,用来获取指定位置的Pager对象。
imgs数组定义及实现:
复制代码
1 List
2 //初始化欢迎界面图片数组
3 imgs = new ArrayList
4 imgs.add(R.drawable.help1);
5 imgs.add(R.drawable.help2);
6 imgs.add(R.drawable.help3);
7 imgs.add(R.drawable.help4);
复制代码
WelcomeFragment类定义
复制代码
1 public class WelcomeFragment extends Fragment {
2
3 View view = null;
4 int imgId ;
5 @Override
6 public View onCreateView(LayoutInflater inflater, ViewGroup container,
7 Bundle savedInstanceState) {
8 view = inflater.inflate(R.layout.welcome_fragment, null);
9
10 ImageView fragmentVw = (ImageView) view.findViewById(R.id.welcome_Img);
11 fragmentVw.setBackgroundResource(imgId);
12 return view;
13 }
14
15 /**
16 * 为该Fragment设置显示图片
17 * */
18 public void setImg(int imgID){
19
20 imgId = imgID;
21 }
22 }
复制代码
WelcomeFragment布局文件
复制代码
1
评论