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);