更新时间:2022-12-11 17:29:46
如果你真的相信有很多静片段在内存消耗大量的内存,也许你可以尝试这样的事:
If you are really sure that having lots of static fragments in memory consumes a huge amount of memory, maybe you could try something like this:
而不是使用的:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position);
}
其实你可以声明地图片段。其主要目标是重新使用previously创建片段,比保持宣称的新实例的。首先,宣布该code对片段的活动(不在SectionsPagerAdapter):
You could actually declare a Map of fragment. The main goal is to re-use previously created fragments, than keep declaring new instance. Firstly, declare this code on fragment's activity (NOT in SectionsPagerAdapter):
private Map<Integer, PlaceholderFragment> mPlaceHolderFragmentArray = new LinkedHashMap<Integer, PlaceholderFragment>();
然后更换的getItem(INT位置)方法SectionsPagerAdapter本:
Then replace getItem(int position) method in SectionsPagerAdapter with this:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
PlaceholderFragment fragment = mPlaceHolderFragmentArray.get(position);
if(fragment == null){
fragment = PlaceholderFragment.newInstance(position);
mPlaceHolderFragmentArray.put(position, fragment);
}
return fragment;
}
我不知道是否有什么更好的办法,但目前我使用这个对我的codeS。
I don't know if there are any better way, but I'm currently using this on my codes.