0

在我的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

你应该关心的一件事是,当你从代码中选择创建选项卡时,所有事件应该被触发,当手动选择选项卡时会触发。 – 2014-11-09 03:26:28

回答

0

虽然我仍然不知道为什么我的代码之前是行不通的 - 我没有发现添加的标签与的setSelected = 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);