Android Fragment解析:深入骨髓

本文详细解析了Android中的Fragment,包括其定义、在Activity中的使用,如静态和动态添加,以及Fragment管理器的使用,如替换、隐藏和显示Fragment。此外,还介绍了Fragment的多返回栈管理和如何为Fragment提供依赖项,帮助开发者更好地理解和掌握Fragment的运用。
摘要由CSDN通过智能技术生成

Fragment

定义:

Fragment是一种嵌套在Activity中的片段,Activity主要负责一个界面,而Fragment是Activity中的片段,用于管理部分界面,可以在Activity中组合多个Fragment,或者在Activity中重复使用某个Fragment,Fragment具有自己的生命周期,能接收自己的输入事件,并且可以在运行时动态的添加或者删除Fragment。Fragment具有自己的生命周期,但是Fragment依托于Activity,所以生命周期受Activity生命周期的影响,Activity被称为宿主

在Activity中使用Fragment

build.gradle中导入依赖:

implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'

1.静态添加:

1.宿主activity_main的布局

使用<androidx.fragment.app.FragmentContainerView>标签添加fragment,其中的name属性用于添加fragment的子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
>
    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/TopFragment"
            android:layout_width="match_parent"
            android:name="com.example.fragment.TopFragment"
            android:layout_height="0dp"
            android:layout_weight="1"
    />
    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/BottomFragment"
            android:name="com.example.fragment.BottomFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
    />
</LinearLayout>

2.创建子布

2.创建子布局

TopFragment的布局:

<?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">
    <TextView android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:textColor="@color/white"
              android:background="@color/purple_200"
              android:text="Top"
    />
</LinearLayout>

创建BottomFragment的布局:

<?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">
    <TextView android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:textColor="@color/white"
              android:background="@color/teal_200"
              android:text="bottom"
</LinearLayout>

3.创建片段类:

要创建Fragment必须扩展Fragment类,将布局传入给构造函数:

class TopFragment: Fragment(R.layout.top_fragment)
class BottomFragment:Fragment(R.layout.bottom_fragment)

4.将Fragment添加到Activity中

1.通过xml的方式,更新main_activity,要在布局中显示Fragment请使用<androidx.fragment.app.FragmentContainerView>标签,这是一种包括单个视图的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
>
    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/TopFragment"
            android:layout_width="match_parent"
            android:name="com.example.fragment.TopFragment"
            android:layout_height="0dp"
            android:layout_weight="1"
    />
    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/BottomFragment"
            android:name="com.example.fragment.BottomFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
    />
</LinearLayout>

添加属性android:name指定Gragment实例化的类名,当活动运行时,Fragment类将会被实例化。

2.通过程序的方式动态添加:删除掉其中的name属性取消

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值