Android开发-java版:Framgent

一、Framgent的简单用法

1.创建两个layout布局文件

java 复制代码
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LeftFragment">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="178dp"
        android:layout_gravity="center_horizontal"
        android:text="button"
        android:textSize="20sp" />

</LinearLayout>

2.引入这个两个布局文件

在主活动布局文件中用<fragment>标签引入这个两个布局文件

java 复制代码
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.myapplication.LeftFragment" />

    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.myapplication.RightFragment" />

</LinearLayout>

3.创建对应的两个java文件

继承Fragment类,导入的是

复制代码
import androidx.fragment.app.Fragment;
java 复制代码
public class LeftFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

二、动态添加Fragment

1.修改主活动布局文件

右侧从<fragment>标签改为<FrameLayout>

java 复制代码
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.myapplication.LeftFragment" />

    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

2.修改主活动文件

1. 获取 FragmentManager

复制代码
FragmentManager fragmentManager = getSupportFragmentManager();
  • 作用:获取 Fragment 的管理器

  • 说明

    • getSupportFragmentManager() 来自 AppCompatActivity

    • 负责管理 Activity 中的 Fragment(添加、移除、替换等操作)

    • 使用 Support 库版本以保证向后兼容

2. 开始事务

复制代码
FragmentTransaction transaction = fragmentManager.beginTransaction();
  • 作用:开始一个 Fragment 事务

  • 说明

    • 所有 Fragment 操作(添加、移除、替换等)必须在事务中执行

    • 事务确保多个操作要么全部成功,要么全部失败

    • 类似于数据库事务的概念

3. 执行替换操作

复制代码
transaction.replace(R.id.right_layout, fragment);
  • 作用:用新的 Fragment 替换指定容器中的当前 Fragment

  • 参数说明

    • R.id.right_layout容器视图的 ID(通常是一个 FrameLayout)

    • fragment要替换进去的新 Fragment 实例

  • 替换过程

    1. 移除容器中当前所有的 Fragment

    2. 添加新的 Fragment 到容器中

4. 提交事务

复制代码
transaction.commit();
  • 作用:提交事务,使替换操作生效

  • 说明

    • 事务只有在调用 commit() 后才会执行

    • 提交后,Fragment 的替换会立即安排执行(可能在主线程空闲时)

java 复制代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        replaceFragment(new RightFragment());
    }

    @Override
    public void onClick(View v) {
        if(R.id.button == v.getId()){
            replaceFragment(new AnotherRightFragment());
        }else{
            return;
        }
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);
        transaction.commit();
    }
}

三、模拟返回栈效果

现在运行程序之后,点击按钮,然后点击导航返回会直接退出程序,可以添加一行代码在点击导航返回的时候返回上一个碎片

在事务中添加:

复制代码
transaction.addToBackStack(null);
相关推荐
firewood20246 分钟前
共射三极管放大电路相关情况分析
笔记·学习
zl0_00_06 分钟前
美亚2023
学习
AI_567810 分钟前
SQL性能优化全景指南:从量子执行计划到自适应索引的终极实践
数据库·人工智能·学习·adb
小小小米粒14 分钟前
Maven Tools
java
zl0_00_015 分钟前
pctf wp
学习
Hello_Embed22 分钟前
libmodbus STM32 主机实验(USB 串口版)
笔记·stm32·学习·嵌入式·freertos·modbus
学编程的闹钟23 分钟前
98【html的php化】
学习
kali-Myon29 分钟前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
我是咸鱼不闲呀32 分钟前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
risc12345637 分钟前
思维脚手架
笔记