2013-02-08 124 views
1

我的Android應用程序擁有兩個片段 1.左一個(這是一個菜單列表) 2.右鍵一個(即左側菜單中的一個項目的細節)標籤的片段

這個問題我我正面臨的是在每個細節屏幕上添加標籤(正確的一個) 我搜索了很多標籤「片段內的片段」或「如何添加tabhost在片段」等

請幫我。 我陷入了它! :-(

回答

1

林面臨着同樣的問題,該解決方案適用於我:

MainActivity - 該活動包含了動作條選項卡(不tabhost)

片段1(TAB1)

Fragment2(TAB2) - 這片段裏面有1個LinearLayout在左側顯示權重= 1的菜單,右側顯示其他LinearLayout以顯示細節。在右邊的linearLayout中,我已經設計了一個Radiogroup來顯示類似標籤的radiogroup(如下所示:https://github.com/makeramen/android-segmentedradiobutton,但帶有tabs資源)然後我將更改linearlayouts以模擬細節片段中的選項卡。

我希望你有想法

+0

感謝提示先生! 是的,我有想法,但請讓人想清楚。 您正在製作的左側菜單是基於按鈕的,還是左側菜單的左側添加了單獨的操作欄? 希望你得到的問題。 –

+0

其實我的主要活動只包含兩個片段! 「 」我想要在右邊詳細視圖中的每個片段中的一個製表符欄「 –

+0

左側菜單是一個列表視圖,右側佈局有radiogroup,我替換」內部「線性佈局,但你可以做出你想要的;) – PaNaVTEC

1

使用最新的支持庫,您可以在Fragment中使用ViewPager,ViewPager可以在類似標籤的視圖中顯示片段(例如Play Store中的示例),ViewPager中的「Tabs」也是片段。

+0

謝謝幫助兄弟! –

0

試試這個。

public class TabBar extends FragmentActivity implements 
      TabHost.OnTabChangeListener { 

     public static Context mContext; 
     private TabHost mTabHost; 
     private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabBar.TabInfo>(); 
     private TabInfo mLastTab = null; 

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

     } 

     class TabFactory implements TabContentFactory { 

      private final Context mContext; 

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


     protected void onSaveInstanceState(Bundle outState) { 
      outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab 
                    // selected 
      super.onSaveInstanceState(outState); 
     } 

     private void initialiseTabHost(Bundle args) { 
      mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
      mTabHost.setup(); 
      // mTabHost.getTabWidget().setStripEnabled(false); 

      TabInfo tabInfo = null; 
      TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab1"), (tabInfo = new TabInfo("Tab1", MyMapFragment.class, args)), "Favourite", 
        R.drawable.frd_tab_select_custom); 
      this.mapTabInfo.put(tabInfo.tag, tabInfo); 
      TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab2"), (tabInfo = new TabInfo(
          "Tab2", PrflTab.class, args)), "Favourite", 
        R.drawable.me_tab_select_custom); 
      this.mapTabInfo.put(tabInfo.tag, tabInfo); 
      TabBar.addTab(this, this.mTabHost, 
        this.mTabHost.newTabSpec("Tab3"), (tabInfo = new TabInfo("Tab3", RequestFragment.class, args)), "Favourite", 
        R.drawable.request_tab_select_custom); 
      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 View prepareTabView(Context context, String text, 
       int drawable) { 
      View view = LayoutInflater.from(context).inflate(
        R.layout.tab_indicator, null); 
      ((ImageView) view.findViewById(R.id.icon)).setImageResource(drawable); 
      return view; 

     } 

     private static void addTab(TabBar activity, TabHost tabHost, 
       TabHost.TabSpec tabSpec, TabInfo tabInfo, String title, int drawable) { 
      // Attach a Tab view factory to the spec 
      tabSpec.setContent(activity.new TabFactory(activity)); 
      String tag = tabSpec.getTag(); 
      View view = prepareTabView(tabHost.getContext(), title, drawable); 
      tabSpec.setIndicator(view); 

      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); 
      boolean flag = activity.getIntent().getBooleanExtra("notifi_falg", false); 
      if (flag) { 
       tabHost.setCurrentTab(2); 
      } 
     } 


     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.tab_1, newTab.fragment, newTab.tag); 
        } else { 
         ft.attach(newTab.fragment); 
        } 
       } 

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

     @Override 
     public void onStop() { 
      super.onStop(); 
     } 
    } 
+0

在你的代碼中! 許多事情都不起作用。 與setContentView()函數類似,它拒絕調用該函數,因爲該類擴展了FragmentActivity .. 此外,「findViewById()」函數不起作用。 此外,我沒有檢查。 –