• 引导页使用ViewPager遇到OutofMemoryError的解决方案


    在开发中需要用到引导页, 用的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.         }; 
  • 相关阅读:
    PR工具栏(选择、轨道选择、剃刀工具)
    PPT动画与多媒体制作
    小微企业增值税申报操作流程
    标题样式
    样式排版
    文本框排版
    使用SmartArt
    插图片与调整
    表格的运用
    Word标尺与段落
  • 原文地址:https://www.cnblogs.com/rosepotato/p/3115600.html
Copyright © 2020-2023  润新知