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);
相关推荐
皮皮林5511 天前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河1 天前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程1 天前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅1 天前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者1 天前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺1 天前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart1 天前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP1 天前
MyBatis-mybatis入门与增删改查
java
孟陬2 天前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端