閱讀485 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在ActionBar中進行Fragment之間的切換

在ActionBar中添加標簽(Tabs),每個標簽對應的是一個Fragment,點擊不同的Tab時,就會切換到對應的Fragment。

  這裏有五個關鍵步驟:

  1. 要實現 ActionBar.TabListener接口,當點擊Tab的時候觸發這個接口裏麵的事件,有onTabSelected()onTabUnselected(), 和 onTabReselected(). 實現ActionBar.TabListener接口時,應當在類內有個Fragment的引用,這樣點擊這個Tab時就可以調用對應的Fragment.

  2. 通過getActionBar() 方法得到Activity中的ActionBar。

  3. 設置AcitonBar的操作模式: setNavigationMode(NAVIGATION_MODE_TABS)

  4. 在ActionBar中添加Tabs:一.調用AciontBar的newTab()生成一個ActionBar.Tab二.為Tab增加text或者icon .調用setText() , setIcon() 三.為每個 ActionBar.Tab 添加ActionBar.TabListener.

  5. 調用addTab()將生成的Tab加入ActionBar中

  

  以下是例子代碼,就是為了測試,沒有實際的用途.

  有兩個Fragment:EditFragment和ComputerFragment,對應的Tab是"編輯"和計算。他們的XML和Activity如下:

 

EditFragment:

public class EditFragment extends Fragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        System.out.println("EidtFragment--->onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        System.out.println("EidtFragment--->onCreateView");
        return inflater.inflate(R.layout.editfragment, container, false);
    }

    @Override
    public void onStop()
    {
        System.out.println("EidtFragment--->onStop");
        super.onStop();
    }
}

editfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="請輸入你的信息:"
        android:textSize="20dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="40pt"
        android:layout_margin="5dp"
        android:background="@android:color/darker_gray"
        android:textSize="18dp" />

</LinearLayout>

ComputerFragment:

public class ComputerFragment extends Fragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        System.out.println("ComputerFragment--->onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        System.out.println("ConputerFragment--->onCreateView");
        return inflater.inflate(R.layout.computerfragment, container, false);
    }

    @Override
    public void onStop()
    {
        System.out.println("ConputerFragment--->onStop");
        super.onStop();
    }
}

computerfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="簡單加法計算"
        android:textSize="20dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@android:color/darker_gray" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@android:color/darker_gray" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="開始計算" />

</LinearLayout>

主要的程序:MainActivity:

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        System.out.println("MainActivity--->onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 得到Activity的ActionBar
        ActionBar actionBar = getActionBar();
        // 設置AcitonBar的操作模型
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // 將Activity的頭部去掉
        actionBar.setDisplayShowTitleEnabled(false);
        // 生成Tab
        Tab edit = actionBar.newTab().setText("編輯");
        Tab computer = actionBar.newTab().setText("計算");
        // 為每個Tab添加Listener
        MyTabListener editListener = new MyTabListener(new EditFragment());
        edit.setTabListener(editListener);
        MyTabListener computerListener = new MyTabListener(new ComputerFragment());
        computer.setTabListener(computerListener);
        // 將Tab加入ActionBar中
        actionBar.addTab(edit);
        actionBar.addTab(computer);
    }

    @Override
    protected void onStop()
    {
        System.out.println("MainActivity--->onStop");
        super.onStop();
    }

    /**
     * 實現ActionBar.TabListener接口
     */
    class MyTabListener implements TabListener
    {
        // 接收每個Tab對應的Fragment,操作
        private Fragment fragment;

        public MyTabListener(Fragment fragment)
        {
            this.fragment = fragment;
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft)
        {

        }

        // 當Tab被選中的時候添加對應的Fragment
        public void onTabSelected(Tab tab, FragmentTransaction ft)
        {
            ft.add(R.id.context, fragment, null);
        }

        // 當Tab沒被選中的時候刪除對應的此Tab對應的Fragment
        public void onTabUnselected(Tab tab, FragmentTransaction ft)
        {
            ft.remove(fragment);
        }
    }
}

程序運行結果:

     


最後更新:2017-04-02 16:47:43

  上一篇:go JavaMail學習筆記(四)、使用POP3協議接收並解析電子郵件(全)
  下一篇:go Android Fragments 詳細使用