Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

ActionBar 實作系列(三):動態切換 Tabs 標籤

編輯:Android 開發實例

上一篇示範了如何建立基本的 Tabs,這次把操作層面補齊:新增標籤、移除標籤、在 Tabs 模式與標準模式之間切換,以及一次清空所有標籤。

Activity 程式碼

每個 Tab 都掛上一個 TabListener,並綁定一個承載內容的 Fragment。這裡把 Fragment 設計成可帶入文字,切換時 onCreateView() 會把文字填進 TextView,方便看出目前是哪一頁。

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

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

    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

上半部留給 Fragment 的 FrameLayout,下半部擺四顆按鈕分別觸發四個操作。這裡用 layout_weight 讓兩塊各佔一半,也可以依實際需求調整比例。

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

Tab 內容配置 action_bar_tab_content.xml

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

實作上的提醒

onTabSelected 與 onTabUnselected 都拿到同一個 FragmentTransaction,因此新增與移除都應該交給它處理,不要自己另外呼叫 commit(),否則容易出現狀態不一致。另外若要在同一個 Tab 上重複點擊時做刷新,就寫在 onTabReselected 裡,這是系統特地保留給「重新選取」的回呼。

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