现在很多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);}MyVIEwPagerAdapterpublic 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,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<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,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(),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组件的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)