Android FragmentTabHost 示例

在Android 3.0以后 Tabhost被标记为Deprecated,官方建议我们使用FragmentTabHost,这里用 FragmentTabHost +Fragment 来实现之前已经实现过的 新浪微博布局效果。


关于FragmentTabHost 的说明可以参考 http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html





maintabs.xml

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_weight="0.0"  
            android:visibility="gone"/>

        <RelativeLayout
            android:id="@+id/layout_bottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/radiogroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@drawable/tabbar_background"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/radio_home"
                    style="@style/maintabt"
                    android:drawableTop="@drawable/icon_home"
                    android:text="主页" />

                <RadioButton
                    android:id="@+id/radio_msg"
                    style="@style/maintabt"
                    android:drawableTop="@drawable/icon_meassage"
                    android:text="消息" />

                <RadioButton
                    android:id="@+id/radio_profile"
                    style="@style/maintabt"
                    android:drawableTop="@drawable/icon_profile"
                    android:text="我" />

                <RadioButton
                    android:id="@+id/radio_square"
                    style="@style/maintabt"
                    android:drawableTop="@drawable/icon_square"
                    android:text="广场" />

                <RadioButton
                    android:id="@+id/radio_more"
                    style="@style/maintabt"
                    android:drawableTop="@drawable/icon_more"
                    android:text="更多" />
            </RadioGroup>
        </RelativeLayout>
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>



FragmentTabsActivity.java

public class FragmentTabsActivity extends FragmentActivity implements
		OnCheckedChangeListener {

	private static final String TAG = FragmentTabsActivity.class
			.getSimpleName();
	private FragmentTabHost mTabHost;
	private RadioGroup radioGroup;

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

		findView();
	}

	private void findView() {
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTabHost.getTabWidget().setVisibility(View.GONE); // 隐藏系统的TabWidget

		mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"),
				HomeFragment.class, null);

		mTabHost.addTab(mTabHost.newTabSpec("message").setIndicator("Message"),

		MessageFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("profile").setIndicator("Profile"),
				ProfileFragment.class, null);

		mTabHost.addTab(mTabHost.newTabSpec("square").setIndicator("Square"),
				SquareFragment.class, null);

		mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator("More"),
				MoreFragment.class, null);

		// mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTabByTag("home");
		((RadioButton) findViewById(R.id.radio_home)).setChecked(true);

		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		radioGroup.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		FragmentManager fm = getSupportFragmentManager();
		HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag("home");
		MessageFragment msgFragment = (MessageFragment) fm
				.findFragmentByTag("message");
		ProfileFragment profileFragment = (ProfileFragment) fm
				.findFragmentByTag("profile");
		SquareFragment squareFragment = (SquareFragment) fm
				.findFragmentByTag("square");
		MoreFragment moreFragment = (MoreFragment) fm.findFragmentByTag("more");

		FragmentTransaction ft = fm.beginTransaction();

		// ** Detaches the androidfragment if exists */
		if (homeFragment != null)
			ft.detach(homeFragment);

		if (msgFragment != null)
			ft.detach(msgFragment);

		if (profileFragment != null)
			ft.detach(profileFragment);

		if (squareFragment != null)
			ft.detach(squareFragment);

		if (moreFragment != null)
			ft.detach(moreFragment);

		switch (checkedId) {
		case R.id.radio_home:
			if (homeFragment == null) {
				/** Create AndroidFragment and adding to fragmenttransaction */
				ft.add(R.id.realtabcontent, new HomeFragment(), "home");
			} else {
				/**
				 * Bring to the front, if already exists in the
				 * fragmenttransaction
				 */
				ft.attach(homeFragment);
			}
			mTabHost.setCurrentTabByTag("home");
			break;
		case R.id.radio_msg:
			if (msgFragment == null) {
				ft.add(R.id.realtabcontent, new MessageFragment(), "message");
			} else {
				ft.attach(msgFragment);
			}
			mTabHost.setCurrentTabByTag("message");
			break;
		case R.id.radio_profile:
			if (profileFragment == null) {
				ft.add(R.id.realtabcontent, new ProfileFragment(), "profile");
			} else {
				ft.attach(profileFragment);
			}
			mTabHost.setCurrentTabByTag("profile");
			break;
		case R.id.radio_square:
			if (squareFragment == null) {
				ft.add(R.id.realtabcontent, new SquareFragment(), "square");
			} else {
				ft.attach(squareFragment);
			}
			mTabHost.setCurrentTabByTag("square");
			break;
		case R.id.radio_more:
			if (moreFragment == null) {
				ft.add(R.id.realtabcontent, new MoreFragment(), "more");
			} else {
				ft.attach(moreFragment);
			}
			mTabHost.setCurrentTabByTag("more");
			break;
		default:
			break;
		}
		ft.commitAllowingStateLoss();
	}

}

FragmentTabHost 用法和TabHost 用法差不多,自己看看官方文档就明白了。


工程下载地址:http://download.csdn.net/detail/fx_sky/6608547


更正:

onCheckedChanged方法里面,最后是需要ft.commit();这句话的,由于个人马虎给大家造成误解表示道歉。完整代码如下:

@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		FragmentManager fm = getSupportFragmentManager();
		HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag("home");
		MessageFragment msgFragment = (MessageFragment) fm
				.findFragmentByTag("message");
		ProfileFragment profileFragment = (ProfileFragment) fm
				.findFragmentByTag("profile");
		SquareFragment squareFragment = (SquareFragment) fm
				.findFragmentByTag("square");
		MoreFragment moreFragment = (MoreFragment) fm.findFragmentByTag("more");

		FragmentTransaction ft = fm.beginTransaction();

		// ** Detaches the androidfragment if exists */
		if (homeFragment != null)
			ft.detach(homeFragment);

		if (msgFragment != null)
			ft.detach(msgFragment);

		if (profileFragment != null)
			ft.detach(profileFragment);

		if (squareFragment != null)
			ft.detach(squareFragment);

		if (moreFragment != null)
			ft.detach(moreFragment);

		switch (checkedId) {
		case R.id.radio_home:
			if (homeFragment == null) {
				/** Create AndroidFragment and adding to fragmenttransaction */
				ft.add(R.id.realtabcontent, new HomeFragment(), "home");
			} else {
				/**
				 * Bring to the front, if already exists in the
				 * fragmenttransaction
				 */
				ft.attach(homeFragment);
			}
			mTabHost.setCurrentTabByTag("home");
			break;
		case R.id.radio_msg:
			if (msgFragment == null) {
				ft.add(R.id.realtabcontent, new MessageFragment(), "message");
			} else {
				ft.attach(msgFragment);
			}
			mTabHost.setCurrentTabByTag("message");
			break;
		case R.id.radio_profile:
			if (profileFragment == null) {
				ft.add(R.id.realtabcontent, new ProfileFragment(), "profile");
			} else {
				ft.attach(profileFragment);
			}
			mTabHost.setCurrentTabByTag("profile");
			break;
		case R.id.radio_square:
			if (squareFragment == null) {
				ft.add(R.id.realtabcontent, new SquareFragment(), "square");
			} else {
				ft.attach(squareFragment);
			}
			mTabHost.setCurrentTabByTag("square");
			break;
		case R.id.radio_more:
			if (moreFragment == null) {
				ft.add(R.id.realtabcontent, new MoreFragment(), "more");
			} else {
				ft.attach(moreFragment);
			}
			mTabHost.setCurrentTabByTag("more");
			break;
		default:
			break;
		}
		ft.commitAllowingStateLoss();
	}






评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值