TabHost选项卡的实现(二):使用Fragment实现

简介:

在上一篇博客《TabHost选项卡的实现(一):使用TabActivity实现》中,讲解了如何使用TabActivity创建管理选项卡,但是,通过TabActivity创建选项卡的方式已经不再推荐使用,Android3.0之后推出Fragment,并推荐我们使用Fragment来实现标签页。关于Fragment的使用,可以参考我之前的几篇博文:

1. Fragment学习(一) :生命周期

2. Fragment开发实战(一)

3. Fragment学习(二): 管理Fragment和Fragment通讯

4. Fragment开发实战(二)


如果已经对Fragment很了解了,那接下来,我们介绍,如何使用Fragment来实现TabHost,效果图如下:



开发过程:

首先,我们需要定义一个Activity,该Activity管理了社会新闻、生活新闻、娱乐新闻、军事新闻这四个子布局,也就是Fragment。我们先定义该Activity的布局界面:

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tb1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="社会新闻" />

        <TextView
            android:id="@+id/tb2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="生活新闻" />

        <TextView
            android:id="@+id/tb3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="娱乐新闻" />

        <TextView
            android:id="@+id/tb4"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="军事新闻" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

我们可以看出,程序使用四个TextView代表了效果图上的四个Tab标签页,在实际的开发中,我们可以为该标签做一些效果,使其更加美观,此处介绍不再详细介绍。

在页面布局的下面,我们定义了一个id为content的LinearLayout布局,该布局负责动态替换Fragment的布局。


接下来,我们定义四个Fragment,每个Fragment管理一个子布局,因为Demo里每个Fragment都相似,此处只贴出一个Fragment1.java的代码:

package com.chen.yuan.fragment;

import com.chen.yuan.R;

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

public class Fragment1 extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment1, null);
    }
}


该Fragment管理的布局文件为fragment1.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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="社会新闻"
        android:textColor="#ff00ff"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
         />

</LinearLayout>

四个标签页,对应于四个Fragment,而Fragment应该归于Activity管理,我们使用Fragment动态的管理Fragment:

public class MainActivity extends Activity implements OnClickListener
{
    private TextView tv1 = null;

    private TextView tv2 = null;

    private TextView tv3 = null;

    private TextView tv4 = null;

    private FragmentManager fm = null;

    private FragmentTransaction ft = null;

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

        tv1 = (TextView) findViewById(R.id.tb1);
        tv2 = (TextView) findViewById(R.id.tb2);
        tv3 = (TextView) findViewById(R.id.tb3);
        tv4 = (TextView) findViewById(R.id.tb4);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);

        fm = getFragmentManager();
        ft = fm.beginTransaction();
        
        ft.replace(R.id.content, new Fragment1());
        ft.commit();
    }

    @Override
    public void onClick(View v)
    {
        ft = fm.beginTransaction();
        switch (v.getId())
        {
            case R.id.tb1:

                ft.replace(R.id.content, new Fragment1());

                break;
            case R.id.tb2:
                ft.replace(R.id.content, new Fragment2());
                break;
            case R.id.tb3:
                ft.replace(R.id.content, new Fragment3());
                break;
            case R.id.tb4:
                ft.replace(R.id.content, new Fragment4());
                break;

            default:
                break;
        }
        ft.commit();
    }
}

代码具体啥意思,我就不再说明了,建议先学会Fragment的基本使用再看该例子,很简单。


代码下载地址免费: http://download.csdn.net/detail/zuiwuyuan/7986609


相关文章
|
17天前
自定义tablayout,好用
自定义tablayout,好用
11 0
|
11月前
|
XML Java 数据格式
Fragment底部导航栏
一个页面以微信为例,从上到下依次是状态栏,Activity顶部导航栏,Fragment,Activity底部导航栏。 每点击一个底部导航栏都会replace另一个Fragment。
40 0
|
12月前
|
容器
Fragment——底部导航栏的实现
本节开始我们会讲解一些Fragment在实际开发中的一些实例!而本节给大家讲解的是底部导航栏的实现!而基本的底部导航栏方法有很多种,比如全用TextView做,或者用RadioButton,又或者使用TabLayout + RadioButton,当然复杂的情况还是得走外层套布局的方法!本节我们用TextView来做一个底部导航栏的效果,也熟悉下Fragment的使用!
72 0
|
Android开发 容器 数据格式
TabLayout的自定义
TabLayout的自定义,主要是通过setCustomView方法来添加自定义布局实现。 自定义TabLayout的实现主要包含以下几个步骤 ●创建自定义布局(这里我加了一个动画控件,可以替换成其他控件) ...
1949 0
|
Android开发
Android--在非activity弹出Dialog对话框
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/80521795 需要在adapter类监听button点击事件弹出需要弹出一个系统级对话框,也就是这个对话框不论是在哪个活动,都可以弹出这个对话框。
2844 0
|
XML Android开发 数据格式
Android--FragmentTabHost+ViewPager+Fragment实现底部tab菜单栏
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/79001632 activity_main.
1193 0
|
Android开发 Java 容器
|
XML Android开发 数据格式
xamarin Tablayout+Viewpager+Fragment顶部导航栏
最近几天不忙,所以把项目中的顶部导航栏的实现归集一下。android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单。当然我这个导航栏是基于xamarin android的。
2151 0
|
Android开发 容器 Java
Android BottomSheet:底部弹出Fragment面板(4)
 Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出“重”的fragment,但是此fragment是BottomSheetFragment。
2074 0