Unlike the baseadapter of listview/gridview, The pageradapter of viewpager does not have an internal view reuse mechanism. That is to say, after inflate and call destroyitem, the view is discarded, if you need more views, You need to inflate them again. If all views in viewpager are basically the same, there will be a waste of memory. Here we use a very simple method to reuse the View:
List<View> mViewList = new ArrayList<View>();public Object instantiateItem(View container, int position) { View view = null; if (mViewList.isEmpty) { inflate a new View } else { view = mViewList.remove(0); } ........}public void destroyItem(View container, int position, Object object) { View view = (View)object; ((ViewPager) container).removeView(view); mViewList.add(view); ........}
In fact, it is very easy to define a list to recycle the view. When destroyitem is used, the view is added to the list. When the inflate is required, first, judge whether there are recycled views in the list. If so, reuse them. This solves the problem of constantly inflate views and then destroy.