详解Android App中创建ViewPager组件的方法
现在很多app一打开就是一个viewpager,然后可以用手指滑,每滑一次就换一张图,底下还会有圈圈表示说现在滑到第几章~
通常这些图片都是放功能简介或是使用教学之类的,我的需求很简单,就是上面提到的那样而已。
有两种做法,一种是找现有套件,查了一堆资料每个都跟我推荐viewpagerindicator这套,我之前也看过这套,只是看起来需要有fragment再加上google play范例好像载不到了,所以只好自己实做一个。
viewpager的实作可参考android viewpager使用详解里面的程序码:
@overrideprotected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_intro); mviewpager = (viewpager) findviewbyid(r.id.viewpager); final layoutinflater minflater = getlayoutinflater().from(this); view v1 = minflater.inflate(r.layout.intro_layout_1, null); view v2 = minflater.inflate(r.layout.intro_layout_2, null); view v3 = minflater.inflate(r.layout.intro_layout_3, null); view v4 = minflater.inflate(r.layout.intro_layout_4, null); viewlist = new arraylist<view>(); viewlist.add(v1); viewlist.add(v2); viewlist.add(v3); viewlist.add(v4); mviewpager.setadapter(new myviewpageradapter(viewlist)); mviewpager.setcurrentitem(0);} myviewpageradapter public class myviewpageradapter extends pageradapter { private list<view> mlistviews; public myviewpageradapter(list<view> mlistviews) { this.mlistviews = mlistviews; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); } @override public object instantiateitem(viewgroup container, int position) { view view = mlistviews.get(position); container.addview(view); return view; } @override public int getcount() { return mlistviews.size(); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0==arg1; }}
这样子你就有一个viewpager了
下面我们来具体看一下viewpager的用法:
一、viewpager创建步骤
① 在xml布局中加入android.support.v4.view.viewpager
② 加载显示的页卡将layout布局转换为view对象
(1)
layoutinflater lf getlayoutinflater().from(this); lf.inflate(resource,root);
(2)
view.inflate(context,resource,root);
③ 配置adapter(三种adapter)
(1)pageradapter 数据源:list<view>
(2)fragmentpageradapter 数据源:list<fragment>
(3)fragmentstatepageradapter 数据源:list<fragment>
二、代码示例
新建四个fragment和他们的布局下面是mainactivity的xml布局:
<android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" > <android.support.v4.view.pagertabstrip android:id="@+id/tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" > </android.support.v4.view.pagertabstrip> <!-- 底部显示标题与上面的顶部显示不能同时出现 <android.support.v4.view.pagertitlestrip android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" > </android.support.v4.view.pagertitlestrip> --> </android.support.v4.view.viewpager>
第一种pageradapter
public class mypageradapter extends pageradapter { private list<view> viewlist; private list<string> titlelist; public mypageradapter(list<view> viewlist,list<string> titlelist){ this.viewlist = viewlist; this.titlelist = titlelist; } /** * 返回页卡的数量 */ @override public int getcount() { return viewlist.size(); } /** * view是否来自对象 */ @override public boolean isviewfromobject(view arg0, object arg1) { return arg0==arg1; } /** * 实例化一个页卡 */ @override public object instantiateitem(viewgroup container, int position) { container.addview(viewlist.get(position)); return viewlist.get(position); } /** * 销毁一个页卡 */ @override public void destroyitem(viewgroup container, int position, object object) { container.removeview(viewlist.get(position)); } /** * 设置viewpager的标题 */ @override public charsequence getpagetitle(int position) { return titlelist.get(position); } }
第二种fragmentpageradapter
public class myfragmentpageradapter extends fragmentpageradapter { private list<fragment> fraglist; private list<string> titlelist; public myfragmentpageradapter(fragmentmanager fm,list<fragment> fraglist,list<string> titlelist) { super(fm); this.fraglist = fraglist; this.titlelist = titlelist; } @override public fragment getitem(int arg0) { return fraglist.get(arg0); } @override public charsequence getpagetitle(int position) { return titlelist.get(position); } @override public int getcount() { return fraglist.size(); } }
第三种fragmentstatepageradapter(该适配器可以动态销毁fragment)
public class myfragmentpageradapter2 extends fragmentstatepageradapter {//该适配器可以动态销毁 private list<fragment> fraglist; private list<string> titlelist; public myfragmentpageradapter2(fragmentmanager fm,list<fragment> fraglist,list<string> titlelist) { super(fm); this.fraglist = fraglist; this.titlelist = titlelist; } @override public fragment getitem(int arg0) { return fraglist.get(arg0); } @override public charsequence getpagetitle(int position) { return titlelist.get(position); } @override public int getcount() { return fraglist.size(); } @override public object instantiateitem(viewgroup arg0, int arg1) { return super.instantiateitem(arg0, arg1); } @override public void destroyitem(viewgroup container, int position, object object) { super.destroyitem(container, position, object); } }
mainactivity里使用getsupportfragmentmanager()该activity必须继承fragmentactivity:
public class mainactivity extends fragmentactivity implements onpagechangelistener{ private list<view> viewlist; private list<string> titlelist; private viewpager pager; private pagertabstrip tab;//顶部标题 private list<fragment> fraglist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewlist = new arraylist<view>(); titlelist = new arraylist<string>(); tab = (pagertabstrip) findviewbyid(r.id.tab); view view1 = view.inflate(this,r.layout.view1,null); view view2 = view.inflate(this,r.layout.view2,null); view view3 = view.inflate(this,r.layout.view3,null); view view4 = view.inflate(this,r.layout.view4,null); viewlist.add(view1); viewlist.add(view2); viewlist.add(view3); viewlist.add(view4); fraglist = new arraylist<fragment>(); fraglist.add(new fragment1()); fraglist.add(new fragment2()); fraglist.add(new fragment3()); fraglist.add(new fragment4()); //为viewpager页卡设置标题 titlelist.add("第一页"); titlelist.add("第二页"); titlelist.add("第三页"); titlelist.add("第四页"); //为pagertabstrip设置一些属性 tab.setbackgroundcolor(color.white); tab.setdrawfullunderline(false); tab.settabindicatorcolor(color.blue); pager = (viewpager) findviewbyid(r.id.pager); //mypageradapter adapter = new mypageradapter(viewlist,titlelist); //myfragmentpageradapter adapter = new myfragmentpageradapter(getsupportfragmentmanager(), fraglist, titlelist); /** * 使用getsupportfragmentmanager()该activity必须继承fragmentactivity */ myfragmentpageradapter2 adapter = new myfragmentpageradapter2(getsupportfragmentmanager(), fraglist, titlelist); pager.setadapter(adapter); pager.setonpagechangelistener(this); } @override public void onpagescrollstatechanged(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpageselected(int arg0) { toast.maketext(this,"当前是第"+(arg0+1)+"个界面", 0).show(); } }
推荐阅读
-
详解Android App中创建ViewPager组件的方法
-
详解Android App中ViewPager使用PagerAdapter的方法
-
详解Android中fragment和viewpager的那点事儿
-
详解Android中Fragment的两种创建方式
-
Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法详解
-
详解Android开发中硬件加速支持的使用方法
-
Android中通过样式来去除app的头及界面全屏(备忘)的实现方法
-
简述Android中实现APP文本内容的分享发送与接收方法
-
详解Android应用开发中Intent的作用及使用方法
-
详解Android中App的启动界面Splash的编写方法