In order to have a list of cached items by ViewPager I changed my Custom Adapter which an extension FragmentStatePagerAdapter:
- Add a
HashMap<Integer, FragmentDipsplayPageContent> cachedFragmentHashMap
to my adapter Update getItem() method like this
public Fragment getItem(int index) { Fragment fragment = new FragmentDipsplayPageContent(); Bundle args = new Bundle(); args.putInt("INDEX", index); fragment.setArguments(args); cachedFragmentHashMap.put(index,fragment); return fragment; }
Update destroyItem() method of adapter like this
@Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); //add this line to remove fragments wiped from ViewPager cache cachedFragmentHashMap.remove(position); }
Add a getter to access HashMap of cached Fragments from activity:
public HashMap<Integer, FragmentDipsplayPageContent> getCachedFragmentHashMap() { return cachedFragmentHashMap; }
update using this collection inside activity:
private void increaseFontSizeForCachedFragments() { HashMap<Integer, FragmentDipsplayPageContent> cachedFragmentsHashMap = adapterViewPager .getCachedFragmentHashMap(); Collection<FragmentDipsplayPageContent> fragmentsCollection = cachedFragmentsHashMap .values(); for (FragmentDipsplayPageContent fragmentDipsplayPageContent : fragmentsCollection) { //update views of fragment fragmentDipsplayPageContent.increasTextFontSize(); } }
This way all cached fragments including visible and off-screen fragments are updated.