用Fragment来代替过期的Tabhost和ActivityGroup

我去年9月份做的一个项目中,主界面也是选项卡这种效果,搭建项目的另一位工程师仍然使用的是Tabhost。

最近想起这件事来,便有了这篇博客。(Tabhost和ActivityGroup分别在api 13 和 14 中标记为过期)

本篇博客主要介绍如何用Fragment实现Tabhost效果。


下图红色部分是RelativeLayout,用来实现Fragment的容器;绿色部分为RadioGroup。


首先布局xml中加入一个RelativeLayout来作为Fragment的容器,相当于Tabhost中的tabcontent;加入RadioGroup来作为选项卡,相当于Tabhost中的TabWidget。具体如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!-- 相当于TabWidget -->

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="一" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="二" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="三" />
    </RadioGroup>
    <!-- 相当于tabcontent -->

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/radioGroup1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </RelativeLayout>

</RelativeLayout>


其次,在当前Activity中加入默认Fragment,并且通过RadioGroup的监听来进行切换Fragment。具体Activity代码如下:

package com.kenneth.blogtabfragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * @author Kenneth 2014-2-11 上午10:26:06
 */
public class MainActivity extends Activity implements OnCheckedChangeListener {
	public static final String TAG = MainActivity.class.getSimpleName();
	private Context mContext = this;
	private FragmentManager fm;
	private RadioGroup radioGroup1;
	private RadioButton radio0;
	private RadioButton radio1;
	private RadioButton radio2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
		initListener();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void initView() {
		radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
		radio0 = (RadioButton) findViewById(R.id.radio0);
		radio1 = (RadioButton) findViewById(R.id.radio1);
		radio2 = (RadioButton) findViewById(R.id.radio2);
	}

	private void initData() {
		fm = getFragmentManager();// 获取fragment管理工具类
		replace(new FirstFragment());//默认选第一个
	}

	private void initListener() {
		radioGroup1.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radio0:
			replace(new FirstFragment());
			break;
		case R.id.radio1:
			replace(new SecondFragment());

			break;
		case R.id.radio2:
			replace(new ThirdFragment());

			break;

		default:
			break;
		}
	}

	private void replace(Fragment fragment) {//替换当前容器中的Fragment
		FragmentTransaction beginTransaction = fm.beginTransaction();
		beginTransaction.replace(R.id.content, fragment);
		beginTransaction.commit();
	}
}

以上,就是用Fragment来实现Tabhost效果的主要代码。如果是api11以下,请把getFragmentManager替换为getSupportFragmentManager,其他Fragment相关的请引v4支持包中的。

用多了你就会发现Fragment要比Tabhost,ActivityGroup好用太多了。

我的工程包地址。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值