我正在使用带有FragmentStatePagerAdapter
(有很多页面)的ViewPager导致内存问题。
这很有效,但是我希望能够在重新创建片段时将页面(ListFragments
)恢复到原始位置。这似乎很简单,但我没有太多运气实现它。
有没有人有任何提示?
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment fragment = new PostListFragment();
Bundle args = new Bundle();
Map<String, String> categoryitem=categoryList.get(position);
args.putString("slug", categoryitem.get("slug"));
fragment.setArguments(args);
return fragment;
}
在PostListFragment中,我调用Web服务来获取基于&#34; slug&#34;的帖子。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Bundle bundle=this.getArguments();
slug=bundle.getString("slug");
Log.v("PostList Fragment of"+slug,"onCreateView");
getPosts(slug);
View view=inflater.inflate(R.layout.fragment_post_list, container, false);
progressDialog = (ProgressBar)view.findViewById(R.id.progressBar);
postRecyclerView=(RecyclerView) view.findViewById(R.id.postRecyclerview);
postRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
postRecyclerView.removeAllViews();
return view;
}
每次调用webservice时都在这里。我想仅针对特定的slug调用webservice一次,然后存储该slug的帖子并重新使用这些帖子而不再调用webservice。
public class MainFragment extends Fragment {
HomePagerAdapter pagerAdapter;
SlidingTabLayout slidingTabs;
ViewPager viewPager;
MyDb db;
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
pagerAdapter=new HomePagerAdapter(getChildFragmentManager() ,getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_main, container,false);
slidingTabs=(SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
viewPager=(ViewPager) view.findViewById(R.id.vpPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
slidingTabs.setCustomTabView(R.layout.custom_tab, R.id.title);
db=new MyDb(getActivity());
slidingTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
// TODO Auto-generated method stub
return getResources().getColor(R.color.red);
}
});
slidingTabs.setViewPager(viewPager);
return view;
}
}
我的FragmentPagerAdapter类
public class HomePagerAdapter extends FragmentStatePagerAdapter {
Context context;
private SharedPreferences mySharedpref;
String userId;
MyDb db;
FragmentManager mFragmentManager;
ArrayList<Map<String, String>> categoryList = new ArrayList<Map<String, String>>();
public HomePagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context=context;
this.mFragmentManager = fm;
mySharedpref = PreferenceManager.getDefaultSharedPreferences(context);
userId=mySharedpref.getString("userId", "0");
db=new MyDb(context);
categoryList = db.getCategoryFromDb("30");
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment fragment = new PostListFragment();
Bundle args = new Bundle();
Map<String, String> categoryitem=categoryList.get(position);
args.putString("slug", categoryitem.get("slug"));
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return categoryList.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
// return super.getPageTitle(position);
Map<String, String> categoryTitle=categoryList.get(position);
return categoryTitle.get("title");
// return title[position];
}
}