Android fragment的使用案例

效果图:两个点击事件,显示不同的fragment布局
默认是如下图,点击页面一也如下图

点击页面二如下图:

Android Fragment的生命周期是与其所在的Activity紧密相关的。当一个Fragment被添加到Activity中时,它将经历一系列的生命周期回调方法。以下是Fragment生命周期的主要阶段

  • onAttach():当Fragment与Activity关联时调用。在此方法中,Fragment可以访问它所在的Activity实例,例如通过getActivity()方法。
  • onCreate():创建Fragment时调用。在此方法中,应初始化Fragment所需的组件。
  • onCreateView():Fragment需要创建视图时调用。在此方法中,Fragment应该初始化它的UI组件并返回一个View对象,这个View对象将成为Fragment的根视图。如果Fragment不提供UI,可以返回null。
  • onActivityCreated():当Activity的onCreate()方法完成后调用。此时,Fragment可以与Activity中的视图进行交互。
  • onStart():Fragment变为可见时调用。
  • onResume():Fragment开始与用户交互时调用。此时,Fragment可以接收用户输入。
  • onPause():Fragment将要停止与用户交互时调用。在此方法中,应提交任何需要持久化的更改。
  • onStop():Fragment不再可见时调用。
  • onDestroyView():Fragment的视图被销毁时调用。此时,应释放与视图相关的所有资源。
  • onDestroy():销毁Fragment时调用。在此方法中,应清理Fragment持有的所有资源。
  • onDetach():Fragment与Activity解除关联时调用。

需要注意的是,Fragment的生命周期受其所属的Activity影响。例如,当Activity进入暂停状态时,其中的所有Fragment也会进入暂停状态。当Activity被销毁时,所有的Fragment也会被销毁。

Java代码实现

public class MainActivity extends AppCompatActivity {
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg=findViewById(R.id.rg);
        FrameLayout fragment=findViewById(R.id.fragment);
        //默认显示的Fragment
        fragment.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //getSupportFragmentManager 管理器
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment,new FirstLayout());
                transaction.commit();
                fragment.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        //点击选中
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()){
                    case R.id.rb1:
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fragment,new FirstLayout());
                        transaction.commit();
                        break;
                    case R.id.rb2:
                        FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
                        transaction1.replace(R.id.fragment,new SecondFragment());
                        transaction1.commit();
                        break;
                }
            }
        });

    }
}
  • xml
<?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=".MainActivity6">
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="页面一"
            android:background="@color/bgdialogpress"
            />
        <RadioButton
            android:id="@+id/rb2"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="页面二"
            android:background="@color/bgdialogpress"
            />
    </RadioGroup>
    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>
  • 下面列举 FirstLayout 、 xml布局,SecondFragment和下面相似,自己建一个就行
//Java处理
public class FirstLayout extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_first, container, false);
        return inflate;
    }
}
//xml布局
<RelativeLayout 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:padding="16dp"
    tools:context=".MainActivity">
    <Button
        android:layout_centerInParent="true"
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面一"/>
</RelativeLayout>

相关推荐

  1. C# 接口使用案例

    2024-03-11 01:46:01       64 阅读
  2. thinkphp结合rabbitmq使用案例

    2024-03-11 01:46:01       56 阅读
  3. yarn安装以及使用案例

    2024-03-11 01:46:01       35 阅读
  4. c++中缓冲器使用案例

    2024-03-11 01:46:01       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-11 01:46:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-11 01:46:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-11 01:46:01       82 阅读
  4. Python语言-面向对象

    2024-03-11 01:46:01       91 阅读

热门阅读

  1. Linux 内核中处理阻塞访问的方法:等待队列

    2024-03-11 01:46:01       47 阅读
  2. 自定义组件v-model传值方法

    2024-03-11 01:46:01       45 阅读
  3. Android下使用OpenOCD

    2024-03-11 01:46:01       37 阅读
  4. AJAX-查询参数

    2024-03-11 01:46:01       36 阅读
  5. springboot集成 mongodb以及mongodb简单工具类

    2024-03-11 01:46:01       33 阅读
  6. python xml 解析

    2024-03-11 01:46:01       42 阅读
  7. 设计模式: 模板方法模式

    2024-03-11 01:46:01       44 阅读