Android学习—Fragment的简单使用,并实现TabHost

最近学习了android开发中的Fragment,小编在这里将Fragment的使用方法和利用Fragment实现TabHost的步骤分享给大家

文字较少,并非长篇大论,请大家认真阅读,若文中表述不当,还请大家给予批评和指导,不吝赐教。


Fragment,翻译为“碎片”、“片段”。在整个安卓的学习中,Fragment作为一个相当重要的基础概念,它起到了以下几点作用

  • 降低代码耦合度,替Activity缓解压力
  • 提高代码的可重用性,一个Fragment可用于多个Activity
  • 根据硬件,自适应布局

首先我们要明白其使用思路和流程:

(1)Fragment的使用方法

将Activity划分为多个部分,每个部分都可以作为一个Fragment碎片的容器;创建继承于Fragment的子类,重写onCreateView()方法将该子类的碎片布局填充到container容器中;在Activity中调用Fragment管理器的创建事务方法,将上述创建的Fragment子类利用add()方法添加到所要显示到的区域中,最后执行commit()方法确定提交事务。

(2)利用Fragment实现TabHost功能

       利用上述步骤,创建多个Fragment和对应数量的Button按钮,一一对应,在Button中添加onClick监听事件,若按钮被点击,则生成Fragment管理器,创建事务并调用replace()方法将创建的Fragment添加到所要显示到的区域中,最后执行commit(方法确定提交事务。

话不多说,开始上码


(1)Fragment的使用方法

  • 将Activity划分为多个部分,这里我分成了上下两个部分,上部区域用于填充Fragment_A,下部区域填充Fragment_B。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:weightSum="1"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragmentA_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"></FrameLayout>

    <FrameLayout
        android:id="@+id/fragmentB_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"></FrameLayout>

</LinearLayout>

控件布置如下图所示

  • 创建继承于Fragment的类--AFragment与其XML布局文件,重写onCreateView()方法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Fragment.AFragment"
    android:gravity="center">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="This is A Fragment"
        android:textSize="30dp"></TextView>

</LinearLayout>

                                                                   

public class AFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a,container,false);
        return view;
    }
}

仿照上述代码,创建BFragment类与其XML布局文件,重写其onCreateView()方法。

  • 在MainActiviy中创建Fragment管理器,启动事务,将A、BFragment分别添加到已经分好的fragmentA_container、fragmentB_container中,最后调用commit()方法提交事务。下文代码使用了两种方式,均可使用。
public class MainActivity extends AppCompatActivity {

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

        //I
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragmentA_container,new AFragment());
        ft.add(R.id.fragmentB_container,new BFragment());
        ft.commit();

        //II
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentA_container,new 
        AFragment()).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentB_container,new     
        BFragment()).commit();
    }
}

运行代码,效果如下图所示

这里我们就完成了Fragment的基本使用,下面讲述如何利用Fragment实现TabHost。


(2)利用Fragment实现TabHost

  • 将Activity划分为两个部分,上部分区域用来填充Fragment,下部分区域则是两个Button按钮,我们将利用这两个按钮实现Fragment的切换,即实现了TabHost。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:weightSum="1"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"></FrameLayout>

    <LinearLayout
        android:id="@+id/Buttons"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:orientation="horizontal"
        android:weightSum="1">
        <Button
            android:id="@+id/btn_a"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="A"
            android:textSize="30dp"></Button>
        <Button
            android:id="@+id/btn_b"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:text="B"
            android:textSize="30dp"></Button>
    </LinearLayout>

</LinearLayout>

控件布置如下图所示

  • 在MainActivity中,为ButtonA和B增加onClickListner监听事件,重写onClick()方法,根据控件标识,创建Fragment管理器,启动事务,使用replace()方法替换当前fragment_container中的Fragment,最后调用commit()方法提交事务。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

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

        //进入界面首先显示AFragment
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container,new AFragment());
        ft.commit();

        button_a = findViewById(R.id.btn_a);
        button_b = findViewById(R.id.btn_b);

        button_a.setOnClickListener(this);
        button_b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_a:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new AFragment()).commit();
                break;
            case R.id.btn_b:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new BFragment()).commit();
                break;
        }
    }
}
  • 效果如下图所示

到这里我们就完成了利用Fragment 实现TabHost,Ohhhhh!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值