10 使用ViewPager实现导航
10-1 认识ViewPager
ViewPager可以使视图左右引动
ViewPager的数据源相当于View或者Fragment对象的一个集合
VierPager就是一个容器,里面可以装View或者fragment两类东西
1.加入ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager>
android.support.v4.view.viewPager 为了解决兼容的问题。
android.support.v4是谷歌公司为了解决当前版本碎片化的问题,从而提供的一个兼容的包。可以实现向下兼容的问题,让一些高版本的控件能够在低版本中进行使用;
2.加载显示的页卡
将Layout布局转化为View对象
(1)LayoutInflater inflater=getLayoutInflater().from(this);
inflater.inflate(resource, root);
(2)View.inflate(context, resource, root);
3.配置适配器Adapter
(1)PagerAdapter 数据源:List<View>
(2)FragmentPagerAdapter: List<Fragment>
(3)FragmentStatePagerAdapter List<Fragment>
10-2 适配器PagerAdapter
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </android.support.v4.view.ViewPager> </LinearLayout>
view1.xml以及view2.xml view3.xml view4.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个界面" /> </LinearLayout>
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private ViewPager pager; private List<View> viewList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList); // ViewAdapter加载适配器 pager.setAdapter(adapter); } }
MyPageAdapter.java
package com.example.andriod2_viewpage; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPageAdapter extends PagerAdapter { // 1.适配器的构造方法写出来 private List<View> viewList; public MyPageAdapter(List<View> viewList) { this.viewList = viewList; } /* * 返回页卡的数量 等同于集合的大小 */ @Override public int getCount() { // TODO Auto-generated method stub return viewList.size(); } /* * View是否来之对象 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } /* * 实例化页卡 position是当前的位置 */ @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub // 添加到ViewGroup来 container.addView(viewList.get(position)); return viewList.get(position); } /* * 销毁一个页卡 通过container移除一个View */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewList.get(position)); } }
结果显示:左右界面滑动
10-3 PagerTabStrip和PagerTitleStrip
加标题:
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private ViewPager pager; private List<View> viewList; private List<String> titleList; private PagerTabStrip tabStrip; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); // 为ViewPager页卡设置标题 titleList = new ArrayList<String>(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList, titleList); // ViewAdapter加载适配器 pager.setAdapter(adapter); } }
MyPageAdapter.java
package com.example.andriod2_viewpage; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPageAdapter extends PagerAdapter { // 1.适配器的构造方法写出来 private List<View> viewList; private List<String> titleList; public MyPageAdapter(List<View> viewList, List<String> titleList) { this.viewList = viewList; this.titleList = titleList; } /* * 返回页卡的数量 等同于集合的大小 */ @Override public int getCount() { // TODO Auto-generated method stub return viewList.size(); } /* * View是否来之对象 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } /* * 实例化页卡 position是当前的位置 */ @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub // 添加到ViewGroup来 container.addView(viewList.get(position)); return viewList.get(position); } /* * 销毁一个页卡 通过container移除一个View */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewList.get(position)); } /** * 设置ViewPager页卡的标题 */ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" 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.ViewPager> </LinearLayout>
为PagerTabStrip设置属性
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private ViewPager pager; private List<View> viewList; private List<String> titleList; private PagerTabStrip tabStrip; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); // 为ViewPager页卡设置标题 titleList = new ArrayList<String>(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); // 为PagerTabStrip设置属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setBackgroundColor(Color.YELLOW); tabStrip.setTextColor(Color.RED); // 最长的横线不要 tabStrip.setDrawFullUnderline(false); // 小粗线的颜色 tabStrip.setTabIndicatorColor(Color.GREEN); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList, titleList); // ViewAdapter加载适配器 pager.setAdapter(adapter); } }
-----------------------------------------------------------------------------------------------------
注意:PagerTitleStrip:当有PagerTitleStrip,则PagerTabStrip失效
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" 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> </LinearLayout>
具体步骤:
1 在MainActivity中创建一个对象private PagerTabStrip tab;
2 在main.xml的v4.view.ViewPager中嵌套多一个v4.view.PagerTabStrip
3 在MainActivity中为ViewPager页卡设置标题
private List<String>titleList; //声明
titleList=new ArrayList<String>(); //实例化
titleList.add("第一页");//添加 有好几条
4 把titleList传到构造方法MyPagerAdapter 并且写好
5 MyPagerAdapter中加载getPageTitle方法 来设置ViewPager页卡的标题
要实现这个方法 必须要在布局文件中加载了PagerTabStrip货PagerTitleStrip
6 为PagerTabStrip设置一些属性
7 PagerTitleStrip出现 PagerTabStrip会失效
10-4 适配器FragmentPagerAdapter
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; public class MainActivity extends FragmentActivity { private ViewPager pager; private List<View> viewList; private List<String> titleList; private List<Fragment> fragList; private PagerTabStrip tabStrip; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); /* * 通过Fragment作为ViewPager的数据源 */ fragList = new ArrayList<Fragment>(); fragList.add(new Fragment1()); fragList.add(new Fragment2()); fragList.add(new Fragment3()); fragList.add(new Fragment4()); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); // 为ViewPager页卡设置标题 titleList = new ArrayList<String>(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); // 为PagerTabStrip设置属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setBackgroundColor(Color.YELLOW); tabStrip.setTextColor(Color.RED); // 最长的横线不要 tabStrip.setDrawFullUnderline(false); // 小粗线的颜色 tabStrip.setTabIndicatorColor(Color.GREEN); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList, titleList); // ViewAdapter加载适配器 // pager.setAdapter(adapter); MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter( getSupportFragmentManager(), fragList, titleList); pager.setAdapter(fragmentPagerAdapter); } }
MyFragmentPagerAdapter.java
package com.example.andriod2_viewpage; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.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); // TODO Auto-generated constructor stub this.fragList = fragList; this.titleList = titleList; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return fragList.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return fragList.size(); } /** * 标题信息的返回 */ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } }
效果和使用PageAdapter是一样的,只不过它没有销毁过程。
验证它没有销毁过程:
Fragment1、2、3.java
package com.example.android_viewpager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment3 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view3, container, false); } }
Fragment4.java
package com.example.andriod2_viewpage; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public class Fragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view4,container,false); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("t", "我被销毁了"); } }
在LogCat没有显示。
-------------------------------------------------------------------------
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; public class MainActivity extends FragmentActivity { private ViewPager pager; private List<View> viewList; private List<String> titleList; private List<Fragment> fragList; private PagerTabStrip tabStrip; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); /* * 通过Fragment作为ViewPager的数据源 */ fragList = new ArrayList<Fragment>(); fragList.add(new Fragment1()); fragList.add(new Fragment2()); fragList.add(new Fragment3()); fragList.add(new Fragment4()); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); // 为ViewPager页卡设置标题 titleList = new ArrayList<String>(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); // 为PagerTabStrip设置属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setBackgroundColor(Color.YELLOW); tabStrip.setTextColor(Color.RED); // 最长的横线不要 tabStrip.setDrawFullUnderline(false); // 小粗线的颜色 tabStrip.setTabIndicatorColor(Color.GREEN); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList, titleList); // ViewAdapter加载适配器 // pager.setAdapter(adapter); MyFragmentPagerAdapter adapter2 = new MyFragmentPagerAdapter( getSupportFragmentManager(), fragList, titleList); // pager.setAdapter(adapter2); MyFragmentPagerAdapter2 adapter3 = new MyFragmentPagerAdapter2( getSupportFragmentManager(), fragList, titleList); pager.setAdapter(adapter3); } }
MyFragmentPagerAdapter2.java
package com.example.android_viewpager; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentStatePagerAdapter; import android.view.ViewGroup; 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); // TODO Auto-generated constructor stub this.fragList=fragList; this.titleList=titleList; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return fragList.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return fragList.size(); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub super.destroyItem(container, position, object); } @Override public Object instantiateItem(ViewGroup arg0, int arg1) { // TODO Auto-generated method stub return super.instantiateItem(arg0, arg1); } }
步骤:
1 建立一个Fragment1继承Fragment
2 Fragment1中建立初始化界面的方法onCreateView
3 复制出4个Fragment2,3,4 并修改好布局文件
4 声明Fragment //private List<Fragment>fragList;
5 通过Fragment作为ViewPager的数据源
初始化 //fragList=new ArrayList<Fragment>();
添加 //fragList.add(new Fragment1());
6 配置数据适配器
新建一个类MyFragmentPagerAdapter 继承FragmentPagerAdapter 并实习一些必要的方法
7 使用getPageTitle方法实现标题信息返回
8 在MainActivity加载适配器
初始化MyFragmentPagerAdapter
Vocation加载适配器 //pager.setAdapter(adapter2);
9 MyFragmentPager中返回值设置好
10 为添加页卡的维护和销毁 在MainActivity中设置好新方法 再加载新的适配器
----------------------
总结:
ViewPager
使用fragment作为数据源:
注意凡是涉及到FragmentPagerAdapter的文件都应该导入android.support.v4.app.Fragment包,兼容性更好
1.创建多个Fragment类继承Fragment加载相应的布局文件,注意导入android.support.v4.app.Fragment;
2.在ViewPager容器创建集合List<Fragment>fragList=new ArrayList<Fragment>();并添加到fragList当中
3.配置适配器继承FragmentPagerAdapter
4.适配器类的构造函数需要手动增加参数,FragmentPagerAdapter无法实现自动实例化和销毁的Fragment
5.实例化适配器:主函数应该继承FragmentActivity,兼容性更好,第一个构造函数传递的参数为getSupportFragmentManager()
6.加载适配器,
7.运行出现问题,注意看适配器中的方法是否重写了
8.FragmentStatePagerAdapter具有自动销毁与实例化页卡的功能
FragmentPagerAdater为适配器加载ViewPager
1. Activity布局文件中添加android.support.v4.view.ViewPager
2. 初始化数据源
(1)List<Fragment>数据源
fragList = new ArrayList<Fragment>();
fragList.add(new Fragment1());
fragList.add(new Fragment2());
(2)初始化Tab title数据源
titleList = new ArrayList<String>();
titleList.add("FirstFrag");
titleList.add("SecondFrag");
3. 新建一个FragmentPagerAdapter适配器,并重写构造函数和其他的函数
public MyFragemntPagerAdapter(FragmentManager fm, List<Fragment> fragList, List<String> titleList) {
super(fm);
this.fragList = fragList;
this.titleList = titleList;
}
4. 在Activity中创建适配器,构造函数中将fragList、titleList都传递进去
MyFragemntPagerAdapter adapter = new MyFragemntPagerAdapter(getSupportFragmentManager(), fragList, titleList);
5. 加载适配器
viewPager.setAdapter(adapter);
6. 注意
(1)需要新建几个Fragment类来将Layout布局转换成View对象。最后再由List<Fragment>添加进来。
(2)在FragmentPagerAdapter中,所有的页卡是一次创建的,并不会销毁
(3)FragmentStatePagerAdapter,用法和FragmentPagerAdapter一样,只是需要添加
instantiateItem(),destroyItem(),且不需要改
(4)FragmentStatePagerAdapter的区别在于页卡是每次创建3个
由于FragmentPagerAdapter是将页卡一次性全部加载进来,所以这个适配器的使用就有局限性,当我们的页卡比较少的时候我们就用它,它的另一个替代FragmentStatePagerAdapter,就可以像PagerAdapter那样一次只加载部分的页卡。
自定义适配器继承FragmentPagerAdapter:适配fragment列表。
自定义适配器继承FragmentStatePagerAdapter:需要重写创建和销毁的方法
FragmentPagerAdapter没有动态添加和销毁Fragment的功能
10-5 监听器的使用
OnPageChangeListener()——用来监控ViiewPager滑动到第几页
MainActivity.java
package com.example.andriod2_viewpage; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends FragmentActivity implements OnPageChangeListener { private ViewPager pager; private List<View> viewList; private List<String> titleList; private List<Fragment> fragList; private PagerTabStrip tabStrip; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList = new ArrayList<View>(); /* * 通过View对象去作为ViewPage的数据源 */ 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); /* * 通过Fragment作为ViewPager的数据源 */ fragList = new ArrayList<Fragment>(); fragList.add(new Fragment1()); fragList.add(new Fragment2()); fragList.add(new Fragment3()); fragList.add(new Fragment4()); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); // 为ViewPager页卡设置标题 titleList = new ArrayList<String>(); titleList.add("第一页"); titleList.add("第二页"); titleList.add("第三页"); titleList.add("第四页"); // 为PagerTabStrip设置属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setBackgroundColor(Color.YELLOW); tabStrip.setTextColor(Color.RED); // 最长的横线不要 tabStrip.setDrawFullUnderline(false); // 小粗线的颜色 tabStrip.setTabIndicatorColor(Color.GREEN); // 初始化 ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PageAdapter的适配器 MyPageAdapter adapter = new MyPageAdapter(viewList, titleList); // ViewAdapter加载适配器 // pager.setAdapter(adapter); MyFragmentPagerAdapter adapter2 = new MyFragmentPagerAdapter( getSupportFragmentManager(), fragList, titleList); // pager.setAdapter(adapter2); MyFragmentPagerAdapter2 adapter3 = new MyFragmentPagerAdapter2( getSupportFragmentManager(), fragList, titleList); pager.setAdapter(adapter3); pager.setOnPageChangeListener(this); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub // (arg0+1)从0开始计算的 Toast.makeText(this, "这是第" + (arg0 + 1) + "个界面", Toast.LENGTH_SHORT).show(); } }
切换页卡的监听器:
1.OnPageChangListener,其中最常用的函数是onPageSelected(int arg0);
2.Fragment生命周期比较全,以fragment为资源的ViewPager比较好控制,适用于逻辑比较复杂的情况,如果只是为了展示则推荐以view为资源的viewPager