在开发中需要用到引导页, 用的Google ViewPager类, 采用的方式是在将图片设置于layout,最后加载所有的layout,但是由于加载的较多,由于加载的时候一不小心就报了OutofMemoryError。

layout:

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" 
  6.     android:background="@drawable/page01"  
  7.      > 
  8. </LinearLayout> 

最后优化一下,通过在instantiateItem中逐步加载layout的方式解决了该问题,

因为ViewPager自身有机制,回调destroyItem回收View资源。

PagerAdapter: 

 
  1. @Override 
  2.             publicvoid destroyItem(ViewGroup container, int position, 
  3.                     Object object) { 
  4.                 // TODO Auto-generated method stub 
  5.                 container.removeView((View)object); 
  6.             } 
  7.  
  8.             @Override 
  9.             public Object instantiateItem(ViewGroup container, int position) { 
  10.                 // TODO Auto-generated method stub 
  11.                 LayoutInflater mLayoutInflater = getLayoutInflater(); 
  12.                 int resId=0
  13.                 switch (position) { 
  14.                 case0
  15.                     resId = R.layout.page01; 
  16.                     break
  17.                 case1
  18.                     resId = R.layout.page02; 
  19.                     break
  20.                 case2
  21.                     resId = R.layout.page03; 
  22.                 break
  23.                 case3
  24.                     resId = R.layout.page04; 
  25.                 break
  26.                 case4
  27.                     resId = R.layout.page05; 
  28.                 break
  29.                 case5
  30.                     resId = R.layout.page06; 
  31.                 break
  32.                 case6
  33.                     resId = R.layout.page07; 
  34.                 break
  35.                 default
  36.                     break
  37.                 } 
  38.                 View view = mLayoutInflater.inflate(resId, null); 
  39.                 container.addView(view, 0); 
  40.                 return view; 
  41.             } 
  42.  
  43.         }; 
posted on 2013-06-03 15:53  Rosepotato  阅读(303)  评论(0编辑  收藏  举报