【Android】使用Actionbar Tab导航

直接上代码。

Activity类:

package com.zzj.ui.actionbardemo;

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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zzj.ui.R;

public class ActionbarTabActivity extends Activity implements TabListener {
	private ActionBar actionBar;

	private Fragment aFragment;
	private Fragment bFragment;
	private Fragment cFragment;

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

		System.out.println(this);
		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		/*
		 * 
		 * 切屏(横屏竖屏切换)时Activity会被销毁,
		 * 
		 * 但是Fragment会备份并且重新加载(调用onCreateView()方法)。
		 */
		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		aFragment = getFragmentManager().findFragmentByTag("aFragment");
		if (aFragment == null) {
			aFragment = new MyFragment();
			fragmentTransaction.add(R.id.fragment_container, aFragment,
					"aFragment");
		}
		bFragment = getFragmentManager().findFragmentByTag("bFragment");
		if (bFragment == null) {
			bFragment = new MyFragment();
			fragmentTransaction.add(R.id.fragment_container, bFragment,
					"bFragment");
		}
		cFragment = getFragmentManager().findFragmentByTag("cFragment");
		if (cFragment == null) {
			cFragment = new MyFragment();
			fragmentTransaction.add(R.id.fragment_container, cFragment,
					"cFragment");
		}
		System.out.println("aFragment:" + aFragment.hashCode());
		System.out.println("bFragment:" + bFragment.hashCode());
		System.out.println("cFragment:" + cFragment.hashCode());
		fragmentTransaction.commit();

		Tab weChat = actionBar.newTab();
		weChat.setText("聊天");
		weChat.setTabListener(this);
		weChat.setTag(0);
		actionBar.addTab(weChat);

		Tab findTab = actionBar.newTab();
		findTab.setText("发现");
		findTab.setTabListener(this);
		findTab.setTag(1);
		actionBar.addTab(findTab);

		Tab mailList = actionBar.newTab();
		mailList.setText("通讯录");
		mailList.setTabListener(this);
		mailList.setTag(2);
		actionBar.addTab(mailList);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		switch ((Integer) tab.getTag()) {
		case 0:
			ft.show(aFragment);
			ft.hide(bFragment);
			ft.hide(cFragment);
			break;
		case 1:
			ft.show(bFragment);
			ft.hide(aFragment);
			ft.hide(cFragment);
			break;
		case 2:
			ft.show(cFragment);
			ft.hide(bFragment);
			ft.hide(aFragment);
			break;
		default:
			break;
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	public static class MyFragment extends Fragment {

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			System.out.println(container.getTag() + "_" + this.getTag() + "_"
					+ this.hashCode());
			View view = inflater
					.inflate(R.layout.fragmenttab, container, false);
			TextView textView = (TextView) view
					.findViewById(R.id.fragment_textview);
			textView.setText("fragment_" + this.hashCode());
			return view;
		}
	}
}
主布局文件actionbartab_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:tag="fragment_container" >

</LinearLayout>
fragment布局文件fragmenttab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/fragment_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
我们可以看到,要使用Actionbar 的Tab进行导航,就会使用到fragment。

效果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值