android 中的 ViewPager+ Fragment
android的Viewpager 的各种经常的用法,朋友问我要过,所以就稍微总结一下,
ViewPager + Fragment 经常用到 代码是从 actionbarsherlock 中提取出来的,这个效果都知道是 滑动来切换的。直接上代码了
在这里简单说明一下 FragmentStatePagerAdapter 和 FragmentPagerAdapter
2个adapter
第一种 fragment状态adapter - 在当前只会存在 前1个fragment 当前 fragment 和 下1个 fragment 其他销毁 ,适合加载多数据
第二种 FragmentPagerAdapter - 全部存在,所以不太适合加载 大量的数据 如图片什么的,很容易内存溢出。
工程结构:
1. activity adapter 在一起 (static类型的 Fragment)
- public class ViewPageFragment extends FragmentActivity {
- //这个是有多少个 fragment页面
- static final int NUM_ITEMS = 5;
- private MyAdapter mAdapter;
- private ViewPager mPager;
- private int nowPage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.pagers_fragment_main);
- mAdapter = new MyAdapter(getSupportFragmentManager() );
- mPager = (ViewPager)findViewById(R.id.mypagers_pager);
- mPager.setAdapter(mAdapter);
- }
- /**
- * 有状态的 ,只会有前3个存在 其他销毁, 前1个, 中间, 下一个
- */
- public static class MyAdapter extends FragmentStatePagerAdapter {
- public MyAdapter(FragmentManager fm) {
- super(fm);
- }
- @Override
- public int getCount() {
- return NUM_ITEMS;
- }
- //得到每个item
- @Override
- public Fragment getItem(int position) {
- return ArrayFragment.newInstance(position);
- }
- // 初始化每个页卡选项
- @Override
- public Object instantiateItem(ViewGroup arg0, int arg1) {
- // TODO Auto-generated method stub
- return super.instantiateItem(arg0, arg1);
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- System.out.println( "position Destory" + position);
- super.destroyItem(container, position, object);
- }
- }
- /**
- * 无状态的 会全部加载着, 这个适合少量的 特别多的图片啊啥的 还是用 FragmentStatePagerAdapter
- * @author lilei
- */
- // public static class MyAdapter extends FragmentPagerAdapter {
- // public MyAdapter(FragmentManager fm ) {
- // super(fm);
- //
- // }
- //
- // @Override
- // public int getCount() {
- // return NUM_ITEMS;
- // }
- //
- // @Override
- // public Fragment getItem(int position) {
- // // 返回相应的 fragment
- // return ArrayFragment.newInstance(position);
- // }
- //
- // @Override
- // public void destroyItem(ViewGroup container, int position, Object object) {
- // System.out.println( "position Destory" + position);
- // super.destroyItem(container, position, object);
- // }
- // }
- /**
- * 所有的 每个Fragment
- */
- public static class ArrayFragment extends Fragment {
- int mNum;
- static ArrayFragment newInstance(int num) {
- ArrayFragment array= new ArrayFragment();
- Bundle args = new Bundle();
- args.putInt("num", num);
- array.setArguments(args);
- return array;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mNum = getArguments() != null ? getArguments().getInt("num") : 1;
- System.out.println("mNum Fragment create ="+ mNum);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- System.out.println("onCreateView = ");
- //在这里加载每个 fragment的显示的 View
- View v = null;
- if(mNum == 0){
- v = inflater.inflate(R.layout.pagers_fragment1, container, false);
- ((TextView)v.findViewById(R.id.textView1)).setText(mNum+ "= mNum");
- }else if(mNum == 1){
- v = inflater.inflate(R.layout.pagers_fragment1, container, false);
- ((TextView)v.findViewById(R.id.textView1)).setText(mNum+ "= mNum");
- }else if(mNum == 2){
- v = inflater.inflate(R.layout.pagers_fragment1, container, false);
- ((TextView)v.findViewById(R.id.textView1)).setText(mNum+ "= mNum");
- }else{
- v = inflater.inflate(R.layout.pagers_fragment1, container, false);
- ((TextView)v.findViewById(R.id.textView1)).setText(mNum+ "= mNum");
- }
- return v;
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- System.out.println("onActivityCreated = ");
- super.onActivityCreated(savedInstanceState);
- }
- @Override
- public void onDestroyView(){
- System.out.println(mNum + "mNumDestory");
- super.onDestroyView();
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- }
- }
- }
2. 和 1也没什么太大区别( 个中用处 看个人了 )
- public class ViewPageFragmentCS extends FragmentActivity {
- //这个是有多少个 fragment页面
- private MyAdapter mAdapter;
- private ViewPager mPager;
- private List<Entity> list = new ArrayList<ViewPageFragmentCS.Entity>();;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.pagers_fragment_main);
- for (int i = 0; i < 7 ; i++) {
- Entity ee = new Entity();
- ee.name = "ll"+ i;
- ee.age = ""+ i;
- list.add(ee);
- }
- mAdapter = new MyAdapter(getSupportFragmentManager(),list);
- mPager = (ViewPager)findViewById(R.id.mypagers_pager);
- mPager.setAdapter(mAdapter);
- }
- private class Entity{
- public String name;
- public String age;
- }
- // 在这里你可以传 list<Fragment> 也可以传递 list<Object>数据
- public class MyAdapter extends FragmentStatePagerAdapter {
- List<Entity> list_ee;
- public MyAdapter(FragmentManager fm, List<Entity> ee) {
- super(fm);
- this.list_ee = ee ;
- }
- @Override
- public int getCount() {
- return list_ee.size();
- }
- // 初始化每个页卡选项
- @Override
- public Object instantiateItem(ViewGroup arg0, int position) {
- ArrayFragment ff = (ArrayFragment)super.instantiateItem(arg0, position);
- ff.setThings(list_ee.get(position),position);
- return ff;
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- System.out.println( "position Destory" + position);
- super.destroyItem(container, position, object);
- }
- @Override
- public Fragment getItem(int arg0) {
- // TODO Auto-generated method stub
- return new ArrayFragment();
- }
- }
- /**
- * 所有的 每个Fragment
- */
- public class ArrayFragment extends Fragment {
- private Entity ee;
- private int position;
- public void setThings(Entity ee,int position){
- this.ee =ee ;
- this.position = position;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- System.out.println("onCreateView = ");
- //在这里加载每个 fragment的显示的 View
- View v = inflater.inflate(R.layout.pagers_fragment1, container, false);
- ((TextView)v.findViewById(R.id.textView1)).setText(ee.name+ "= ee.Name -=age"+ ee.age);
- return v;
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- System.out.println("onActivityCreated = ");
- super.onActivityCreated(savedInstanceState);
- }
- @Override
- public void onDestroyView(){
- System.out.println("onDestroyView = "+ position);
- super.onDestroyView();
- }
- @Override
- public void onDestroy(){
- System.out.println("onDestroy = "+ position);
- super.onDestroy();
- }
- }
- }
直接复制过去就可以看效果了 别忘记 V4包,xml 布局文件 自己随便整个吧。
滑动到第3个页面的时候可以看到 第1个页面销毁 第4个生成, 当前存在 2 3 4