在我的onCreate()方法中,我尝试手动选择其中一个标签以强制它预加载,然后手动切换回第一个标签。但是,当我启动时,第一个选项卡会初始显示一个空白屏幕,只有在选择另一个选项卡然后重新选择后才会加载。这是我在onCreate中的代码:
//set up tabs
actionBar.addTab(actionBar.newTab()
.setText("Set Current Location")
.setTabListener(new CustomTabListener<SetCurrentLocationFragment>(currLocFragment,this,SetCurrentLocationFragment.class)),true);
actionBar.addTab(actionBar.newTab()
.setText("Input Trip")
.setTabListener(new CustomTabListener<InputNewTripFragment>(inputNewTripFragment,this,InputNewTripFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("View Trip")
.setTabListener(new CustomTabListener<FoodMilesFragment>(calcMilesFragment,this,FoodMilesFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("Past Trips")
.setTabListener(new CustomTabListener<TripHistoryFragment>(tripHistoryFragment,this,TripHistoryFragment.class)));
actionBar.addTab(actionBar.newTab()
.setText("Trip Map")
.setTabListener(new CustomTabListener<ResultsMapFragment>(resultsMapFragment,this,ResultsMapFragment.class)));
//quickly pre-load the View Trip
actionBar.setSelectedNavigationItem(2);
actionBar.setSelectedNavigationItem(0);
这是我的TabListener
private class CustomTabListener<T extends Fragment> implements TabListener {
private Fragment fragment;
private final Activity mBaseActivity;//activity to attach fragment to
private final Class<T> mFragmentClass;
public CustomTabListener(Fragment fragment, Activity activity, Class<T> fragClass)
{
this.fragment = fragment;
mBaseActivity = activity;
mFragmentClass = fragClass;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null)
{
throw new IllegalArgumentException("Fragment must already be instantiated.");
}
//if the fragment has not yet been added to the activity, add it now
if(fragment.getActivity() == null || !fragment.isAdded())
ft.add(R.id.tabFragmentFrame, fragment);
ft.show(fragment);
fragment.setUserVisibleHint(true);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(fragment != null)
{
ft.remove(fragment);
fragment.setUserVisibleHint(false);
}
}
}
值得一提的是,我的TabListener版本(ft.hide(fragment)
替换ft.remove(fragment)
)导致IllegalStateExceptions - 已添加片段。
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null)
{
throw new IllegalArgumentException("Fragment must already be instantiated.");
}
//if the fragment has not yet been added to the activity, add it now
if(fragment.getActivity() == null || !fragment.isAdded())
ft.add(R.id.tabFragmentFrame, fragment);
ft.show(fragment);
fragment.setUserVisibleHint(true);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(fragment != null)
{
ft.hide(fragment);
fragment.setUserVisibleHint(false);
}
}
答案 0 :(得分:0)
虽然我仍然不知道为什么我之前的代码不起作用 - 我确实发现用tabSelected = true将标签添加到指定的位置确实会使它出现。
//set up tabs
actionBar.addTab(actionBar.newTab()
.setText("Input Trip")
.setTabListener(new CustomTabListener<InputNewTripFragment>(inputNewTripFragment,this,InputNewTripFragment.class)),0);
actionBar.addTab(actionBar.newTab()
.setText("View Trip Footprint")
.setTabListener(new CustomTabListener<FoodMilesFragment>(calcFoodMilesFragment,this,FoodMilesFragment.class)),1);
actionBar.addTab(actionBar.newTab()
.setText("Past Trips")
.setTabListener(new CustomTabListener<TripHistoryFragment>(tripHistoryFragment,this,TripHistoryFragment.class)),2);
actionBar.addTab(actionBar.newTab()
.setText("Trip Map")
.setTabListener(new CustomTabListener<ResultsMapFragment>(resultsMapFragment,this,ResultsMapFragment.class)),3);
//quickly pre-load the Food Miles Trip Footprint
actionBar.setSelectedNavigationItem(1);
actionBar.addTab(actionBar.newTab()
.setText("Set Current Location")
.setTabListener(new CustomTabListener<SetCurrentLocationFragment>(currLocFragment,this,SetCurrentLocationFragment.class)),0,true);