Android-第八节01Fragment详解

在这里插入图片描述

Fragment只会创建出java和xml代码,不会在manifests中注册

Fragment需要依赖于Activity来存活

2.在Activity中嵌入Fragment

在这里插入图片描述

代码:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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”>

<fragment

android:name=“com.hnucm.a_test09.BlankFragment”

android:layout_width=“match_parent”

android:layout_marginTop=“5dp”

android:layout_marginBottom=“5dp”

android:layout_marginLeft=“5dp”

android:layout_marginRight=“5dp”

android:layout_height=“match_parent”/>

</androidx.constraintlayout.widget.ConstraintLayout>

3.设置Fragemnt的页面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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”

android:orientation=“vertical”

tools:context=“.BlankFragment”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“20dp”

android:text=“@string/hello_blank_fragment” />

<Button

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“How are you”

android:textAllCaps=“false”

android:id=“@+id/btn”/>

4.Fragment中只需要保留onCreateView()方法,其余暂时用不到

在这里插入图片描述

5.直接运行发现会报错,报错原因没有给fragment设置id

在这里插入图片描述

6.成功运行:

在这里插入图片描述

三、Fragment生命周期

=============================================================================

在这里插入图片描述

| 生命周期函数 | 相关解释 |

| — | — |

| onAttach() | 关联到Activity的时候调用。如果,需要使用Activity的引用或者使用Activity作为其他操作的上下文,将在此回调方法中实现 |

| onCreate() | 在最初创建Fragment的时候会调用,和Activity的onCreate类似。 |

| onCreateView() | 当第一次绘制Fragment的UI时系统调用这个方法,该方法将返回一个View,如果Fragment不提供UI也可以返回null。注意,如果继承自ListFragment,onCreateView()默认的实现会返回一个ListView,所以不用自己实现。这个函数的Bundle参数和onCretate()函数的Bundle参数是同一个 |

| onActivityCreated() | 执行该方法时,与Fragment绑定的Activity的onCreate方法已经执行完成并返回,在该方法内可以进行与Activity交互的UI操作,所以在该方法之前Activity的onCreate方法并未执行完成,如果提前进行交互操作,会引发空指针异常。 |

| onStart() | 启动Fragment的时候回调,这个时候Fragment可见 |

| onResume() | Fragment变为活动状态获取焦点的时候是回调,这个时候Fragment已经完全展示在前台,并且可以和用户交互 |

| onPause() | 注意这个时候Fragment还是可见的,无法与用户交互 |

| onStop() | Fragment变成不可见的时候调用。这个时候Fragment还是活着的,只是可能别加入到了Fragment的回退栈中 |

| onDestroyView() | Fragment中的布局被移除的时候调用 |

| onDestroy() | Fragment被销毁的时候调用 |

| onDetach() | 和Activity解除关联的时候调用,在onDestroy方法之后调用。 |

四、Fragment生命周期与Activity

======================================================================================

在这里插入图片描述

五、Fragment动态添加

=============================================================================

1.删除掉原来绑定的fragment的id,选择合适自己的布局

在这里插入图片描述

代码:

<?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”

android:orientation=“vertical”

tools:context=“.MainActivity”>

<Button

android:id=“@+id/btn3”

android:layout_width=“match_parent”

android:layout_height=“50dp”

android:textAllCaps=“false”

android:text=“Change”/>

<Button

android:id=“@+id/btn2”

android:layout_width=“match_parent”

android:layout_height=“50dp”

android:textAllCaps=“false”

android:text=“Replace”/>

<FrameLayout

android:id=“@+id/fl”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“@color/purple_200”/>

2.创建一个新的fragment(与上面创建步骤一致)

在这里插入图片描述

3.编辑逻辑代码

在这里插入图片描述

代码:

package com.hnucm.a_test09;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import android.view.SurfaceControl;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值