Accepted answer is nice but you shouldn't cache all the items. Instead, you can use this method:
mViewPager.setOffscreenPageLimit(int);
For example, there are many many items with image and animations. If you cache all of them, this probably will result with an OutOfMemoryError
. To prevent, you can define page limit to be cached.
Edit: Instead HashMap<Integer, Object>
, it is better to use SparseArray<Object>
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…