先介绍下父类PagerAdapter,包含主要方法 instantiateItem初始化每个页面的view,这个用的比较少
viewPager = (ViewPager) findViewById(R.id.viewpager); LayoutInflater inflater=getLayoutInflater(); view1 = inflater.inflate(R.layout.layout1, null); view2 = inflater.inflate(R.layout.layout2,null); view3 = inflater.inflate(R.layout.layout3, null); viewList = new ArrayList<View>();// 将要分页显示的View装入数组中 viewList.add(view1); viewList.add(view2); viewList.add(view3); PagerAdapter pagerAdapter = new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getCount() { return viewList.size(); //界面总的个数 } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); //移除view } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(viewList.get(position)); return viewList.get(position); } }; viewPager.setAdapter(pagerAdapter);
List<Fragment> fragments=new ArrayList<Fragment>(); fragments.add(new Fragment1()); //不同布局的 fragments.add(new Fragment2()); FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments); ViewPager vp = (ViewPager)findViewById(R.id.viewpager); vp.setAdapter(adapter); public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 布局初始化 数据加载之类的操作 View view=inflater.inflate(R.layout.layout2, container, false); return view; } } public class FragAdapter extends FragmentPagerAdapter { private List<Fragment> mFragments; public FragAdapter(FragmentManager fm,List<Fragment> fragments) { super(fm); // TODO Auto-generated constructor stub mFragments=fragments; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return mFragments.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return mFragments.size(); } }
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>(); for (int i = 0; i < 280; i++) { MainFragment first = new MainFragment(); fragmentList.add(first); //布局一样数据不一样的界面 } wikiAdapter = new MyFrageStatePagerAdapter(getSupportFragmentManager()); wikipediaPager.setAdapter(wikiAdapter); public class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter { public MyFrageStatePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return MainFragment.newInstance(position); //为了传递当前界面index便于进行初始化 } @Override public int getCount() { return fragmentList.size(); //总的界面数 } } //自定义获得界面及传入当前界面的值,初始化界面 public class MainFragment extends Fragment { private int mNum; public static MainFragment newInstance(int num) { MainFragment f = new MainFragment(); Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } View view; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.wikipedia_pager_item, container,false); initView(view,MainActivity.wikiList.get(mNum)); return view; } }
FragmentPagerAdapter 继承自 PagerAdapter。相比通用的 PagerAdapter,该类更专注于每一页均为 Fragment 的情况。如文档所述,该类内的每一个生成的 Fragment 都将保存在内存之中,因此适用于那些相对静态的页,数量也比较少的那种;
如需要处理很多页,数据动态性较大,占用内存多的情况,使用FragmentStatePagerAdapter。
当然PagerAdapter也可以处理很多页面的,用循环队列来节省内存,例子如下
viewpager总是会预加载后面的界面,所以要让控件本身去找到对于的view和数据 date.index让对应view加载对应数据
package cn.new1.cloudnurse.pregnancy.main.adapter; public class WikipediaPagerAdapter extends PagerAdapter{ //布局的view private List<ScrollView> contentViewList = new LinkedList<ScrollView>(); private int pagerCount; //循环的个数 Context context; public WikipediaPagerAdapter(Context context, int pagerCount) { this.context = context; this.pagerCount = pagerCount;// * 2; for (int i = 0; i < pagerCount; i++) { ScrollView scrollView = (ScrollView) View.inflate(context, R.layout.wikipedia_pager_item, null); contentViewList.add(scrollView); } } WikiPediaData date; @Override public void draw(int arg0, Object arg1) { // WikiPediaData date; try { JSONObject jsonObject = new JSONObject((String)arg1); date = new WikiPediaData(); date.date = jsonObject.optString("currentDate"); date.babychange = jsonObject.optString("v_baby_changes"); date.momchange = jsonObject.optString("v_mom_changes"); showView(date); } catch (JSONException e) { e.printStackTrace(); } } private void showView(final WikiPediaData date){ //获取对应数据的view然后加载数据显示 ScrollView scrollView = contentViewList.get(date.index%pagerCount); dateTv = (TextView) scrollView.findViewById(R.id.wikipedia_date); babyChangeTv = FindView.byId(scrollView, R.id.wikipedia_baby_change); }); dateTv.setText(date.date); babyChangeTv.setText(date.babychange); } @Override public int getCount() { return PregnancyParam.GESTATIONAL_DAY_COUNT; //无数个确保viewpager的数量可以一直累计 } @Override public boolean isViewFromObject(View view, Object obj) { return view == obj; } private TextView dateTv; //日期 private TextView babyChangeTv; //宝贝变化 public static int indexCurrent = 0; @Override public Object instantiateItem(View view, int index) { //要初始化的view ViewGroup parent = (ViewGroup) contentViewList.get(index%pagerCount).getParent(); WikiPediaDb db = new WikiPediaDb(context); WikiPediaData tmpData = db.getContactList(index+""); if (parent != null) { //循环队列加载 部分内存会相同 最好确保不能相邻 viewpager内存实时缓存3个界面 } else { ((ViewPager) view).addView(contentViewList.get(index%pagerCount),0); } indexCurrent = index%pagerCount; return contentViewList.get(index%pagerCount); } @Override public void destroyItem(View view, int index, Object obj) { ((ViewPager) view).removeView(contentViewList.get(index%pagerCount)); //移除view } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return POSITION_UNCHANGED; } }综合多页面数据变化大的推荐使用FragmentStatePagerAdapter