我正在使用带有5个片段的导航底部。
每个片段都会进行http调用和下载内容,但是在更换片段时,视图被破坏并再次下载。
在我的搜索中,我发现我应该使用setoffscreenpagelimit此方法但是我不使用ViewPager。
我该如何实施类似的事情?
而不是更换片段,而是在片段管理器中添加片段并显示/隐藏它们。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
addFragmentsToManager();
}
private void addFragmentsToManager() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.add(R.id.flContent, tripFragment,
tripFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, notificationFragment,
notificationFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, searchFragment,
searchFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, profileFragment,
profileFragment.getClass().getSimpleName());
fragmentTransaction.hide(tripFragment);
fragmentTransaction.hide(notificationFragment);
fragmentTransaction.hide(searchFragment);
fragmentTransaction.hide(profileFragment);
fragmentTransaction.commit();
}
private void changeTab(int position) {
Fragment fragment;
switch (position) {
fragment = .....// get framgnet from position
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in,
R.anim.fade_out);
fragmentTransaction.hide(prvFragment);
fragmentTransaction.show(fragment).commit();
prvFragment = fragment;
}