Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

caching - Android ViewPager: Update off-screen but cached fragments in ViewPager

I have a ViewPager which contains several TextViews inside its fragment which they have different font sizes. In addition, I got buttons for increase/decreasing font size which calculate font size of each textView by adding its default size plus a value called STEP (which changes by inc/dec font size button).
My problem is if a view is displayed for first time after applying size changes, It will create textViews in desired size during onCreateView() however if fragment was cached already and onCreateView is not called again, I don't know how to update their textViews considering only one of cached fragments is displaying on screen (and i can update it with no problem) but others are not displayed (I don't know if they are attached to activity or not in this case).
In other words how can i detect which fragments are cached by ViewPager and their onCreateView already called (these are fragments which update to their views must be applied). I marked them in light green with question marks in below picture: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...