一.ViewPager是什么
1.大概意思
ViewPager简单来说就是左右滑动切换一些带有数据的页面,那ViewPager具体用在哪里,比如微信主页面切换,朋友圈照片切换都会用到ViewPager。
ViewPager与LsitView一样,都需要通过适配器才能将数据放入ViewPager中,一般可以自定义一个适配器MyAdapter继承PagerAdapter(ViewPager所需的适配器)即可。
ViewPager经常会和Fragment连接在一起,当然将Fragment放入ViewPager中也需要适配器,最常用的适配器有FragmentPagerAdapter和FragmentStatePagerAdapter(这里就不说这了,我讲讲如何写一个简单的ViewPager)。
2.简单概括
ViewPager是v4包中的一个类。
ViewPager继承自ViewGroup,其实是一个容器。
ViewPager类似于ListView,也有自己的适配器,里面用来填充数据页面。
ViewPager一般和Fragment一起使用,它更方面的管理页面中Fragment的生命周期。
二.如何写一个简单的ViewPager
1.简单的布局
这里我将4个View视图放入ViewPager中

View视图的布局,放一个ImageView原件,其他视图布局一样

2.自定义一个类继承PagerAdapter用来填写数据


3.在Activity中的操作
1.声明一些所需的控件、类

2.初始化控件

3.实现监听重写的方法(这里我没有做操作,只是讲解一下方法的用处)

这样就可以实现一个简单的ViewPager,如果觉的我讲的不错可以关注我,谢谢。
网友评论