2012-02-15 52 views
2

我試圖將選項卡添加到現有應用程序以添加更多功能我已經能夠實現選項卡並將所有內容都移至片段。但是,我現在設置的方式並不能保留每個選項卡的堆棧。所以基本上我有一個主要的FrameActivity處理選項卡,並將片段附加到每個選項卡。選項卡和片段,單獨的後端堆棧

在我的研究,我發現這個線程:https://stackoverflow.com/a/7480080/792407

他給出的例子讓有很大的意義,但我似乎無法得到的片段顯示。所以讓我解釋我在做什麼,以確保我理解正確:

我有一個主要的選項卡活動,它擴展了FragmentActivity並處理選項卡。佈局看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 
      <TabWidget 
       android:id="@android:id/tabs" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0" 
       /> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="0"/> 

      <FrameLayout 
       android:id="@+android:id/realtabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1"/> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

在這個活動我初始化我的標籤:

mTabHost = getTabHost();   
Resources res = getResources(); 
Intent intent;   
TabHost.TabSpec spec; 

//search tab 
intent = new Intent().setClass(this, searchFragmentStack.class); 
spec = mTabHost.newTabSpec("search").setIndicator("Search",res.getDrawable(R.drawable.ic_tab_search)).setContent(intent);   
mTabHost.addTab(spec); 

//home tab 
intent = new Intent().setClass(this, homeFragmentStack.class); 
spec = mTabHost.newTabSpec("home").setIndicator("Home",res.getDrawable(R.drawable.ic_tab_home)).setContent(intent);   
mTabHost.addTab(spec); 

我用一下堆棧類,如:

public class searchFragmentStack extends ActivityInTab { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      navigateTo(new search()); 
     } 
} 

的ActivityInTab抽象類是一樣的密碼:

abstract class ActivityInTab extends FragmentActivity { // FragmentActivity is just Activity for the support library. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabs_layout); 
    } 

    /** 
    * Navigates to a new fragment, which is added in the fragment container 
    * view. 
    * 
    * @param newFragment 
    */ 
    protected void navigateTo(Fragment newFragment) { 
     FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction ft = manager.beginTransaction(); 
     ft.add(R.id.content, newFragment); 
     ft.addToBackStack(null); 
     ft.commit(); 
    } 

    @Override 
    public void onBackPressed() { 
     FragmentManager manager = getSupportFragmentManager(); 
     if (manager.getBackStackEntryCount() > 0) { 
      // If there are back-stack entries, leave the FragmentActivity 
      // implementation take care of them. 
      super.onBackPressed(); 
     } else { 
      // Otherwise, ask user if he wants to leave :) 
      //showExitDialog(); 
     } 
    } 

} 

和堆棧的佈局也是基於他的例子:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:isScrollContainer="true"> 
</RelativeLayout> 

而這幾乎是它。我所得到的都是黑屏,這使得我認爲這是一個佈局問題,或者我只是做錯了。這有意義嗎?有沒有更好的辦法?我錯過了什麼嗎?任何幫助將不勝感激。

回答

2

我開始一個虛擬的項目,並從頭開始做的一切後想通了:

我用處理選項卡中的活動需要有一個TabActivity和形式實現,如果我創建使用活動正常標籤,除了我將使用單獨的FragmentActivity「堆棧」來管理每個選項卡中的內容。另外,我用的標籤活動的佈局是錯誤的,我在做什麼,我現在用的:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 

那是兩個大的變化,我已經上傳了的情況下,測試項目中的源有人想引用它用於自己的項目:

Seperate Fragment Stacks in Tabs

編輯

忘了提,塔巴克tivity被棄用,所以人們不應該依賴這種方法爲未來的android版本。我還沒有想出如何使用FragmentActivity實現這一點。也就是說,它適用於我當前的需求,並讓我有時間弄清楚如何以新的方式實現它。

+0

你做了與旋轉的東西嗎?因爲當你在標籤1和片段B中,然後旋轉你回到片段A – Streetboy 2012-04-16 08:53:02

+0

@Streetboy你需要將你的活動片段名保存在onSaveInstanceState中。因此,覆蓋該方法,保存活動片段名稱,並在下次應用程序啓動時,在savedInstanceState包中查找字符串。如果找到它,請切換到該片段屏幕。 – Buffalo 2012-06-28 11:06:28

+2

我從鏈接下載了代碼,但tabActivity已棄用api> 14!有沒有解決方案呢。 – 2012-07-03 07:30:19