android的Viewpager 的各种经常的用法,朋友问我要过,所以就稍微总结一下,
ViewPager + Fragment 经常用到 代码是从 actionbarsherlock 中提取出来的,这个效果都知道是 滑动来切换的。直接上代码了
在这里简单说明一下 FragmentStatePagerAdapter 和 FragmentPagerAdapter
2个adapter
第一种 fragment状态adapter - 在当前只会存在 前1个fragment 当前 fragment 和 下1个 fragment 其他销毁 ,适合加载多数据
第二种 FragmentPagerAdapter - 全部存在,所以不太适合加载 大量的数据 如图片什么的,很容易内存溢出。
工程结构:
1. activity adapter 在一起 (static类型的 Fragment)
class="dp-j" start="1">
- public class ViewPageFragment extends FragmentActivity {
-
-
- 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);
- }
-
-
-
-
-
- public static class MyAdapter extends FragmentStatePagerAdapter {
- public MyAdapter(FragmentManager fm) {
- super(fm);
- }
-
- @Override
- public int getCount() {
- return NUM_ITEMS;
- }
-
-
- @Override
- public Fragment getItem(int position) {
- return ArrayFragment.newInstance(position);
- }
-
-
-
- @Override
- public Object instantiateItem(ViewGroup arg0, int arg1) {
-
- 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);
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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 = ");
-
- 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 {
-
-
- 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;
- }
-
-
-
- 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) {
-
- return new ArrayFragment();
- }
-
- }
-
-
-
-
-
-
- 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 = ");
-
- 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