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>

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值