第四天 framemet初级

本文详细介绍了Fragment在Android开发中的应用,包括其概念、优势、创建方法、生命周期及动态加载技巧。Fragment作为Activity的一部分,提供了更好的模块化、可重用性和适配性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fragment 简介

答:Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大
屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手
机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称
Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有
多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以
把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方
便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使
用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受
到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着
销毁!。

Fragment的优势

模块化(Modularity):我们不必把所有代码全部写在Activity中,而是把代码写在各自的Fragment中。
可重用(Reusability):多个Activity可以重用一个Fragment。
可适配(Adaptability):根据硬件的屏幕尺寸、屏幕方向,能够方便地实现不同的布局,这样用户体验更好。

创建Fragment

右键新建Fragment对象

内部执行的顺序是:
(1).定义一个类, 继承Fragment
(2).重写父类的方法onCreateView()
(3).在onCreateView()方法中, 为Fragment 创建UI界面

加载Fragment的两种方法

静态加载

自动生成XML文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".text.showfragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/context_tv_id"
        android:layout_width="match_parent"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

自动生成JAVA文件

public class showfragment extends Fragment {
private TextView textView;

    public showfragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_showfragment, container, false);
        textView = inflate.findViewById(R.id.context_tv_id);
        Bundle arguments = getArguments();
        if(arguments!=null){
            String key = arguments.getString("key");
            textView.setText(key);
        }
        return inflate;
    }

}
在需要加载fragment的activity中,想使用TextView一样,直接创建一个fragment即可.
已下是MainActivity中的xml文件

动态加载

       linearLayoutId = findViewById(R.id.linear_layout_id);
       fragmentManager= getSupportFragmentManager();
       fragmentTransaction = fragmentManager.beginTransaction();
       fragmentTransaction.add(R.id.linear_layout_id,new showfragment());
       fragmentTransaction.commit();
一定要注意,如果用getSupportFragmentManager()这个方法,就整个项目全部用,
不要在项目里交叉使用getFragmentManager()方法.

add,remove,replace,hide 方法

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentActivity fragmentActivity = new fragmentActivity();
        com.example.class_four.fragment.fragmentActivity fragmentActivity1 = new fragmentActivity();

        fragmentTransaction.add(R.id.fragment_id,fragmentActivity);
         //移除一个Fragment
        fragmentTransaction.remove(fragmentActivity);
        //replace(替换一个布局)执行过程是先 remove 然后在 add.
        fragmentTransaction.replace(R.id.fragment_id,fragmentActivity1);
        
        fragmentTransaction.show(fragmentActivity1);
        //隐藏一个Fragment
        fragmentTransaction.hide(fragmentActivity1);
        //提交事务
		fragmentTransaction.commit();

Fragment的生命周期

在这里插入图片描述
文字描述 生命周期

1.onAttach() :Fragment与Activity有联系。
2.onCreate():创建Fragment
3.onCreateView():创建Fragment视图,尽量不要做耗时操作
4.onActivityCreated():当Activity中的onCreate方法执行完后调用。
5.onStart():启动。
6.onResume():可见
7.onPause():不可见
8.onStop():停止。
9. onDestroyView() :销毁Fragment视图
10.onDestroy():销毁fragment对象
11.onDetach():Fragment和Activity解除关联的时候调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值