ActionBar tab + Fragment

实现ActonBar的tab步骤
1. 激活此项功能
ActionBar actionBar = getActionBar();  
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
2. 实现ActionBar.TabListener接口,为增加tab做准备.实现ActionBar.TabListener接口时,应当在类内有个Fragment的引用,这样点击这个Tab时就可以调用对应的Fragment.
class MyTabListener implements TabListener
    {
    	// 接收每个Tab对应的Fragment,操作
        private Fragment fragment;
        
		public MyTabListener(Fragment fragment) {
			super();
			this.fragment = fragment;
		}
		...
	}
3. 增加tab
Tab tab1 = actionBar.newTab().setText("Tab1");
        Tab tab2 = actionBar.newTab().setText("Tab2");
        // 为每个Tab添加Listener
        MyTabListener tab1Listener = new MyTabListener(new Tab1Fragment());
        tab1.setTabListener(tab1Listener);
        MyTabListener tab2Listener = new MyTabListener(new Tab2Fragment());
        tab2.setTabListener(tab2Listener);
        // 将Tab加入ActionBar中
        actionBar.addTab(tab1);
        actionBar.addTab(tab2);

4. 示例
src/com/wind/actionbar/HelloActionBarActivity.java
package com.wind.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

public class HelloActionBarActivity extends Activity{
    private static final String TAG = "HelloActionBarActivity";
	private ActionBar actionBar;
	protected boolean mFlagDial = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");
        setContentView(R.layout.main);
        actionBar = getActionBar();
        //设置AcitonBar的操作模型
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //去掉title
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        // 生成Tab
        Tab tab1 = actionBar.newTab().setText("Tab1");
        Tab tab2 = actionBar.newTab().setText("Tab2");
        // 为每个Tab添加Listener
        MyTabListener tab1Listener = new MyTabListener(new Tab1Fragment());
        tab1.setTabListener(tab1Listener);
        MyTabListener tab2Listener = new MyTabListener(new Tab2Fragment());
        tab2.setTabListener(tab2Listener);
        // 将Tab加入ActionBar中
        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
    }

    /**
     * 实现ActionBar.TabListener接口
     */
    class MyTabListener implements TabListener
    {
    	// 接收每个Tab对应的Fragment,操作
        private Fragment fragment;
        
		public MyTabListener(Fragment fragment) {
			super();
			this.fragment = fragment;
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.add(R.id.context, fragment, null);
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(fragment);
		}
       
    }
}

src/com/wind/actionbar/Tab1Fragment.java
package com.wind.actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab1Fragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.custom_action_view, container, false);
		//return super.onCreateView(inflater, container, savedInstanceState);
	}

}
src/com/wind/actionbar/Tab2Fragment.java
package com.wind.actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Tab2Fragment extends Fragment {


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.two_button_hor, container, false);
		//return super.onCreateView(inflater, container, savedInstanceState);
	}
}

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
	<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/context"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    </FrameLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值