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);
相关推荐
听风若依9 小时前
数据在内存中的存储-纸面笔记整理
c语言·笔记
xiaominngtongxue9 小时前
从中职技能节看实训教学的组织逻辑
笔记
海绵宝宝转agent9 小时前
操作系统八股开源笔记分享
java·面试
事圆则缓9 小时前
Android 多渠道打包实战:Product Flavors、签名、AAB 与发包校验
android
杨逢昌工厂6S管理9 小时前
109-杨逢昌车间管理负熵循环:三环体系持续输出负熵的机制解析
经验分享·笔记·职场和发展·学习方法
inferno10 小时前
SVN学习笔记(二)
笔记·学习·svn
殷色玫瑰10 小时前
C++ string类详解:常用接口、字符串操作与模拟实现
java·linux·c语言·开发语言·数据结构·c++
CHEEVEN_QY10 小时前
新能源汽车电池托盘FSW量产的工艺稳定性控制
java·前端·汽车
惊讶的猫10 小时前
MCP-server
java
传奇开心果编程10 小时前
【ArkUI进阶练中学】第12课:应用架构演进与遗留系统迁移
学习·ui·华为·harmonyos