Bootstrap

解决ViewPager 内存溢出的问题

如果有100个page页面,如果先生成界面然后在添加到ViewPager里面就会内存溢出。

动态生成就不会出现问题,不用的page页面就销毁掉。

/**
	 * ViewPager适配器
	 */
	private class MyPagerAdapter extends PagerAdapter {
		private List<View> mListViews;
		
		public MyPagerAdpter(List<View> aListViews) {
			this.mListViews = aListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {//销毁没有用的page页面======================================
			((ViewPager) arg0).removeView((View)arg2);
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			View view = null;
		    view =new RingView(HomeActivity.this);//关键地方,动态生成页面。
		    view.setId(arg1);
		   ((ViewPager) arg0).addView(view, 0);
			return view;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}


;