标签:
viewpager循环的简单利用 如viewpager数量很大,BasePagerAdapter不能很大的数据,就可以通过循环利用的方式来解决,viewpager总是会预加载后面的界面,所以要让控件本身去找到对于的view和数据
package cn.new1.cloudnurse.pregnancy.main.adapter; public class WikipediaPagerAdapter extends BasePagerAdapter{ //布局的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; } }
标签:
原文地址:http://my.oschina.net/u/274829/blog/501590