2012-07-21 138 views
0

我正在使用片段並創建了一個帶3個選項卡的tabhost,並在第一個選項卡中放置了一個按鈕。我的問題是,我可以更改爲我創建的按鈕的第二個選項卡嗎?這裏是我的代碼:切換選項卡中的按鈕

private class TabInfo { 
     private String tag; 
     private Class<?> clss; 
     private Bundle args; 
     private Fragment fragment; 

     TabInfo(String tag, Class<?> clazz, Bundle args) { 
      this.tag = tag; 
      this.clss = clazz; 
      this.args = args; 
     } 

    } 

    /** 
    * 
    * @author mwho 
    * 
    */ 
    class TabFactory implements TabContentFactory { 

     private final Context mContext; 

     /** 
     * @param context 
     */ 
     public TabFactory(Context context) { 
      mContext = context; 
     } 

     /** 
     * (non-Javadoc) 
     * 
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String) 
     */ 
     public View createTabContent(String tag) { 
      View v = new View(mContext); 
      v.setMinimumWidth(0); 
      v.setMinimumHeight(0); 
      return v; 
     } 

    } 

    /** 
    * (non-Javadoc) 
    * 
    * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) 
    */ 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabs_layout); 
     initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set 
                       // the 
                       // tab 
                       // as 
                       // per 
                       // the 
                       // saved 
                       // state 
     } 
    } 

    /** 
    * (non-Javadoc) 
    * 
    * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle) 
    */ 
    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab 
                   // selected 
     super.onSaveInstanceState(outState); 
    } 

    /** 
    * Initialise the Tab Host 
    */ 
    private void initialiseTabHost(Bundle args) { 
     mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     TabInfo tabInfo = null; 
     ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost 
       .newTabSpec("Tab1").setIndicator("Home"), 
       (tabInfo = new TabInfo("Tab1", HomeTab.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost 
       .newTabSpec("Tab2").setIndicator("Taping Prep"), 
       (tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     ElastoTabsActivity.addTab(this, this.mTabHost, this.mTabHost 
       .newTabSpec("Tab3").setIndicator("Tab 3"), 
       (tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     // Default to first tab 
     this.onTabChanged("Tab1"); 
     // 
     mTabHost.setOnTabChangedListener(this); 
    } 

    /** 
    * @param activity 
    * @param tabHost 
    * @param tabSpec 
    * @param clss 
    * @param args 
    */ 
    private static void addTab(ElastoTabsActivity activity, TabHost tabHost, 
      TabHost.TabSpec tabSpec, TabInfo tabInfo) { 
     // Attach a Tab view factory to the spec 
     tabSpec.setContent(activity.new TabFactory(activity)); 
     String tag = tabSpec.getTag(); 

     // Check to see if we already have a fragment for this tab, probably 
     // from a previously saved state. If so, deactivate it, because our 
     // initial state is that a tab isn't shown. 
     tabInfo.fragment = activity.getSupportFragmentManager() 
       .findFragmentByTag(tag); 
     if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) { 
      FragmentTransaction ft = activity.getSupportFragmentManager() 
        .beginTransaction(); 
      ft.detach(tabInfo.fragment); 
      ft.commit(); 
      activity.getSupportFragmentManager().executePendingTransactions(); 
     } 

     tabHost.addTab(tabSpec); 
    } 

    /** 
    * (non-Javadoc) 
    * 
    * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String) 
    */ 
    public void onTabChanged(String tag) { 
     TabInfo newTab = this.mapTabInfo.get(tag); 
     if (mLastTab != newTab) { 
      FragmentTransaction ft = this.getSupportFragmentManager() 
        .beginTransaction(); 
      if (mLastTab != null) { 
       if (mLastTab.fragment != null) { 
        ft.detach(mLastTab.fragment); 
       } 
      } 
      if (newTab != null) { 
       if (newTab.fragment == null) { 
        newTab.fragment = Fragment.instantiate(this, 
          newTab.clss.getName(), newTab.args); 
        ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); 
       } else { 
        ft.attach(newTab.fragment); 
       } 
      } 

      mLastTab = newTab; 
      ft.commit(); 
      this.getSupportFragmentManager().executePendingTransactions(); 
     } 
    } 

謝謝!

+0

聽起來可能。你嘗試了什麼?我在代碼中看不到任何按鈕點擊。 – Erol 2012-07-22 02:50:19

+0

對不起。事情是我在與第一個選項卡相關聯的片段中創建了按鈕,並且您知道我很難在活動/片段之間切換。據我所知,這一次我不能使用意圖在片段之間切換,因爲片段不會以這種方式運行,對吧? – 2012-07-22 03:00:12

回答

0

在編寫代碼之前,您是否有機會閱讀TabHost documentary?因爲您已在代碼中使用a function來更改選項卡。

你唯一需要做的就是在你的按鈕的onClick監聽器中實現它。如果你不知道如何處理按鈕點擊,網絡上有很多例子。

+0

如果我太困擾你,我很抱歉。我已經閱讀了紀錄片,唯一不理解的是如何從另一個片段(包含按鈕的片段)調用函數setCurrentTab(index)。非常感謝btw。 – 2012-07-22 06:15:36

+0

這是一個TabHost的功能,所以你需要使用你的TabHost對象。 – Erol 2012-07-22 06:25:56

+0

這聽起來像你沒有自己寫上面的代碼。我建議你在提問關於更詳細的事情之前,先研究你在這裏發佈的代碼。 – Erol 2012-07-22 06:38:01