Android ActionBar 關於tab的應用 以及 TabListener的方法詳解

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

actionBar的tab標籤應用以及TabListener的方法詳解

package com.example.actionBarTest.actionBarTab;import android.app.*;import android.os.Bundle;import com.example.actionBarTest.R;import java.util.ArrayList;import java.util.List;/** * Created by Heyiyong on 2014-5-2 上午9:26. */public class TabActivity extends Activity implements ActionBar.TabListener {    private List<Fragment> fragments;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.tab_main);        //這些都是普通的fragment,裡邊只包含一個textView        HomeFragment homeFragment = new HomeFragment();        MusicFragment musicFragment = new MusicFragment();        VideoFragment videoFragment = new VideoFragment();        fragments = new ArrayList<Fragment>();        fragments.add(homeFragment);        fragments.add(musicFragment);        fragments.add(videoFragment);        ActionBar actionBar = getActionBar();        if (actionBar != null) {            //設定actionBar為tabs模式            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);            //添加幾個標籤,並設定點擊監聽事件            actionBar.addTab(actionBar.newTab().setText("首頁").setTabListener(this));            actionBar.addTab(actionBar.newTab().setText("音樂").setTabListener(this));            actionBar.addTab(actionBar.newTab().setText("視頻").setTabListener(this));        }    }    /**     * 當一個標籤被選擇的時候會調用該方法     * @param tab 點擊選擇的標籤     */    @Override    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {        getFragmentManager().beginTransaction().replace(R.id.tab_main, fragments.get(tab.getPosition())).commit();    }    /**     * 當你選擇了另外一個標籤,之前的標籤就會被unselected,於是這個方法就會被調用     * 例如,你當前的標籤下標為0,現在選擇了下標為1的標籤,那麼這個方法會調用,並且傳入的參數tab是原先的標籤對象     * @param tab 原先的標籤對象     */    @Override    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {        System.out.println("onTabUnselected Position = " + tab.getPosition());    }    /**     * 重複選擇當前的標籤,會調用此方法。     * 例如:當前標籤為第二個,當你用手指又點了第二個,就會調用此方法     * @param tab 當前的標籤對象     */    @Override    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {        System.out.println("onTabReselected position = " + tab.getPosition());    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.