簡體   English   中英

ActionBar旋轉時帶有片段的選項卡

[英]ActionBar Tabs with fragments on rotate

我正在構建一個帶有ActionBar和下面兩個標簽的應用程序。 如果設備/模擬器沒有旋轉,一切正常。 如果旋轉,選項卡狀態會自動切換到tab1(正常,因為onCreate被調用)但內容不會更改。 如果我在新方向中選擇一個選項卡, onCreateView()調用所選FragmentonCreateView()方法,但視圖不會更新(始終保持不變)。 有小費嗎?

代碼。

主要活動:

    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab ATab = actionbar.newTab().setText(R.string.player);
    ActionBar.Tab BTab = actionbar.newTab().setText(R.string.stations);

    Fragment AFragment = new AFragment();
    Fragment BFragment = new BFragment();

    PlayerTab.setTabListener(new MyTabsListener(AFragment));
    StationsTab.setTabListener(new MyTabsListener(BFragment));

    actionbar.addTab(ATab);
    actionbar.addTab(BTab);

使用相同的選項卡顯示簡單的文本視圖。 textview簡單說明選擇了哪個選項卡。

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.a, container, false);
}

上面提到的Fragment布局只包含帶有硬編碼Text的TextView。 (僅用於測試目的)


Main布局如下所示。

<?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" >


    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </LinearLayout>

</LinearLayout>

解決了。 我每次都重新創建片段,不再這樣做了解決它。

我的TabListener和onTabSelected(Tab tab, FragmentTransaction ft)ft.add()更改為ft.replace()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM