Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

詳解 Android ActionBar(三):ActionBar Tabs 標籤的動態切換與管理

編輯:關於 Android 開發

上一節我們示範了如何在 ActionBar 中加入 Tabs 標籤與下拉導航,算是把「有」的問題解決了。這一節要處理的是「怎麼用」——如何動態切換、增刪 Tab,並在 Tab 與 Fragment 之間建立正確的連結關係。

核心觀念:Tab 的本質是 Fragment 的容器

切換 Tab 之所以流暢,關鍵在於每個 Tab 背後都綁著一個 Fragment,透過 ActionBar.TabListener 的三個回呼來控制 Fragment 的進出:

  • onTabSelected():該 Tab 被選中,用 ft.add() 把 Fragment 放進容器。
  • onTabUnselected():該 Tab 失去焦點,用 ft.remove() 把 Fragment 移出,避免畫面堆疊。
  • onTabReselected():使用者重複點擊同一個 Tab,通常拿來做「回到頂部」或提示回饋。

本文的範例把這三件事都做齊:新增 Tab、移除最後一個 Tab、在 Tabs 模式與標準模式之間切換,以及一次清空所有 Tab。

ActionBarTabs 完整程式碼

public class ActionBarTabs extends Activity {

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

    // 動態新增一個 Tab,名稱依目前數量遞增
    public void onAddTab(View v) {
        final ActionBar bar = getActionBar();
        final int tabCount = bar.getTabCount();
        final String text = "Tab " + tabCount;

        bar.addTab(bar.newTab().setText(text)
                .setTabListener(new TabListener(new TabContentFragment(text))));
    }

    // 移除最後一個 Tab
    public void onRemoveTab(View v) {
        final ActionBar bar = getActionBar();
        bar.removeTabAt(bar.getTabCount() - 1);
    }

    // 在 Tabs 導航模式與標準模式之間來回切換
    public void onToggleTabs(View v) {
        final ActionBar bar = getActionBar();

        if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
        } else {
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        }
    }

    // 一次移除所有 Tab
    public void onRemoveAllTabs(View v) {
        getActionBar().removeAllTabs();
    }

    // Tab 監聽器:負責 Fragment 的加入與移除
    private class TabListener implements ActionBar.TabListener {
        private TabContentFragment mFragment;

        public TabListener(TabContentFragment fragment) {
            mFragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.add(R.id.fragment_content, mFragment, mFragment.getText());
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(mFragment);
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
        }
    }

    // 每個 Tab 對應的內容 Fragment
    private class TabContentFragment extends Fragment {
        private String mText;

        public TabContentFragment(String text) {
            mText = text;
        }

        public String getText() {
            return mText;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
            TextView text = (TextView) fragView.findViewById(R.id.text);
            text.setText(mText);
            return fragView;
        }
    }
}

action_bar_tabs.xml

版面分成上下兩塊:上半部是 FrameLayout 容器(id 為 fragment_content),用來承載 Fragment;下半部是四顆操作按鈕,各自透過 android:onClick 對應到 Activity 中的方法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical">

        <Button android:id="@+id/btn_add_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_add_tab"
            android:onClick="onAddTab" />

        <Button android:id="@+id/btn_remove_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_remove_tab"
            android:onClick="onRemoveTab" />

        <Button android:id="@+id/btn_toggle_tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_toggle_tabs"
            android:onClick="onToggleTabs" />

        <Button android:id="@+id/btn_remove_all_tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_remove_all_tabs"
            android:onClick="onRemoveAllTabs" />
    </LinearLayout>

</LinearLayout>

action_bar_tab_content.xml

Tab 的內容頁刻意做得很簡單,只有一個 TextView,用來顯示該 Tab 的名稱,方便我們確認切換是否正確:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

動手實作時,建議先在 onTabReselected() 裡放一個 Toast,這是最快確認「Tab 到底有沒有被切到」的方式。等到行為都符合預期後,再把每個 Tab 換成真正要顯示的內容 Fragment 即可。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved