Android开发——Fragment

news/2024/10/19 9:33:06/

Demo

fragment_blank.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"><TextViewandroid:layout_width="match_parent"android:layout_height="40dp"android:text="@string/app_name"android:id="@+id/tv"></TextView><Buttonandroid:layout_width="match_parent"android:layout_height="40dp"android:id="@+id/btn"/>
</LinearLayout>

fragment_blank.java

package com.example.myapplication;import android.os.Bundle;import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;/*** A simple {@link Fragment} subclass.* Use the {@link BlankFragment#} factory method to* create an instance of this fragment.*/public class BlankFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {if(root==null){root = inflater.inflate(R.layout.fragment_blank,container,false);}textView = root.findViewById(R.id.tv);Button button = root.findViewById(R.id.btn);button.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {textView.setText("OK");}});return root;}private View root;private TextView textView;
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="match_parent"android:layout_width="match_parent"android:id="@+id/main"android:orientation="vertical"><androidx.fragment.app.FragmentContainerView android:name="com.example.myapplication.BlankFragment"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/fg"/></LinearLayout>

绑定fragment 

动态添加

main_activity.mxl

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"android:id="@+id/main"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn1"android:text="@string/app_name"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn2"android:text="@string/bottom_sheet_behavior"/><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/fl"android:background="@drawable/ic_launcher_background"/></LinearLayout>

Activity_main.java

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});Button btn = findViewById(R.id.btn1);btn.setOnClickListener(this);Button btn2 = findViewById(R.id.btn2);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {if(v.getId()==R.id.btn1){replaceFragment(new BlankFragment2());}else{replaceFragment(new ItemFragment());}}// 动态切换private void replaceFragment(Fragment blankFragment2) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.fl,blankFragment2);transaction.commit();}
}

 

点击按钮,Fragment对象会被替换到下方FrameLayout容器上。

除了替换,还有很多操作 Fragment对象的方法。

其中将Fragment压入同一个栈的操作,可以通过按返回键依次出栈:

Activity和Fragment通信(Bundle方案)

MainActivity.java:

public void onClick(View v) {if(v.getId()==R.id.btn1){Bundle bundle = new Bundle();bundle.putString("111","222");BlankFragment2 blankFragment2 = new BlankFragment2();blankFragment2.setArguments(bundle);replaceFragment(blankFragment2);}else{replaceFragment(new ItemFragment());}}

按按钮之后将数据传给Fragment对象。

BlankFragment2.java

 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentView view = inflater.inflate(R.layout.fragment_blank2, container, false);TextView textView = view.findViewById(R.id.fb2);Bundle bundle = this.getArguments();assert bundle != null;String ret = bundle.getString("111");// ret存放的就是传过来的参数的值textView.setText(ret);return view;}
}

生命周期

PS:

onDestroyView函数一般在当前UI界面不需要显示时才会调用。而切回主屏幕不会导致UI销毁所以不调用该函数。onCreateView相反。


http://www.ppmy.cn/news/1427574.html

相关文章

【面试经典 150 | 二叉搜索树】验证二叉搜索树

文章目录 写在前面Tag题目来源解题思路方法一&#xff1a;中序遍历方法二&#xff1a;递归 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带一些对于本题涉及…

使用Unity 接入 Stable-Diffusion-WebUI的 文生图api 并生成图像

使用Unity 接入 Stable-Diffusion-WebUI 文生图生成图像 文章目录 使用Unity 接入 Stable-Diffusion-WebUI 文生图生成图像一、前言二、具体步骤1、启动SD的api设置2、unity 创建生图脚本3、Unity 生图交互配置步骤 1: 创建sdControl步骤2&#xff1a;生成后图片画布步骤3&…

【树莓派学习】hello,world!

系统安装及环境配置详见【树莓派学习】系统烧录及VNC连接、文件传输-CSDN博客 树莓派内置python3&#xff0c;可以直接利用python输出。

solidity入门

Solidity 是以太坊智能合约开发的主要编程语言&#xff0c;支持多种数据类型&#xff0c;其中数组是一种非常常用和灵活的数据结构。在本教程中&#xff0c;我们将深入探讨 Solidity 中数组的各种类型、创建规则以及常见操作。 ### 固定长度数组 固定长度数组在声明时指定了数…

[Linux_IMX6ULL驱动开发]-总线设备驱动模型

目录 框架分层 总线驱动模型实现 上层驱动代码(leddrv.c)的实现以及解析 交叉依赖的避免 下层驱动的设备文件(board_A_led.c)的实现 下层驱动的驱动文件(chip_demo_gpio.c)的实现 框架分层 在之前&#xff0c;我们对于驱动的框架有过两种不同的框架。第一种框架&#xf…

python爬虫-----深入了解 requests 库下篇(第二十五天)

&#x1f388;&#x1f388;作者主页&#xff1a; 喔的嘛呀&#x1f388;&#x1f388; &#x1f388;&#x1f388;所属专栏&#xff1a;python爬虫学习&#x1f388;&#x1f388; ✨✨谢谢大家捧场&#xff0c;祝屏幕前的小伙伴们每天都有好运相伴左右&#xff0c;一定要天天…

拓展网络技能:利用lua-http库下载www.linkedin.com信息的方法

引言 在当今的数字时代&#xff0c;网络技能的重要性日益凸显。本文将介绍如何使用Lua语言和lua-http库来下载和提取LinkedIn网站的信息&#xff0c;这是一种扩展网络技能的有效方法。 背景介绍 在当今科技潮流中&#xff0c;Lua语言以其轻量级和高效的特性&#xff0c;不仅…

JavaScript 作用域链详细解析

JavaScript 作用域链&#xff08;Scope Chain&#xff09;是一种变量和函数查找机制&#xff0c;它决定了在某个执行环境中变量和函数的可访问性。当访问一个变量或函数时&#xff0c;JavaScript 引擎会首先在当前的执行环境中查找&#xff0c;如果找不到则会向上级执行环境中继…