public class MyPagerAdapter extends PagerAdapter { private Activity context; private HashMap<Integer, View> viewMap = new HashMap<Integer, View>(); public MyPagerAdapter(Activity mContext) { this.context = mContext; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); viewMap.remove(arg1);//处理之前的 System.gc();//回收资源 System.out.println("viewMapssize:" + viewMap.size()); } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { return list.size(); } @Override public Object instantiateItem(View arg0, int arg1) { LayoutInflater mInflater = context.getLayoutInflater(); View view1 = mInflater.inflate(R.layout.content_view, null); TextView text = (TextView) view1.findViewById(R.id.textview); text.setText(list.get(arg1)); ((ViewPager) arg0).addView(view1); viewMap.put(arg1, view1); return view1; } @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) { } public View getViewByPosition(int position) { return viewMap.get(position); } }
主要是这个adapter,维护一个list数据组与mapView组,代码的github:https://github.com/wszf/ViewPageDemo