android.app.Fragment在API 级别 11 中 添加,已在API 级别 28 中弃用。
二、Fragment为什么被称为第五大组件
Fragment比Activity更节省内存,其切换模式也更加舒适,使用频率不低于四大组件,且有自己的生命周期,而且必须依附于Activity,不能独立存在。
特点:
-
Fragment依赖于Activity,不能独立存在
-
一个Activity可以有多个Fragment
-
一个Fragment可以被多个Activity重用
-
Fragment有自己的生命周期,并能接收输入事件
-
可以在Activity运行时动态地添加或删除Fragment
三、Activity创建Fragment的方式
静态创建
-
定义Fragment的xml布局文件
-
自定义Fragment类,继承Fragment类或其子类,同时实现onCreateView()方法,在方法中通过inflater.inflate加载布局文件,接着返回其View
-
在需要加载Fragment的Activity对应布局文件中的name属性设为全限定类名,即包名.fragment
-
最后在Activity调用setContentView()加载布局文件即可
样例:
1.定义Fragment的xml布局文件(新建fragment_home.xml和fragment_mine.xml)
fragment_home.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:background=“@color/color_ff0000”
android:gravity=“center”
<TextView
android:id=“@+id/tv_on”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“首页”
android:onClick=“onClick”
android:textColor=“@color/white”
android:textSize=“@dimen/text_size_20”/>
fragment_mine.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:background=“@color/color_188FFF”
android:gravity=“center”
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“我的”
android:textColor=“@color/white”
android:textSize=“@dimen/text_size_20”/>
2.自定义Fragment类,继承Fragment类或其子类,同时实现onCreateView()方法,在方法中通过inflater.inflate加载布局文件,接着返回其View
public class HomeFragment extends Fragment {
private TextView tv_on;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container,false);
MLog.e(this.getClass().getName()+“onCreateView”);
tv_on = view.findViewById(R.id.tv_on);
tv_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MLog.e(this.getClass().getName()+“准备关闭”);
}
});
return view;
}
}
3.在需要加载Fragment的Activity对应布局文件中的name属性设为全限定类名,即包名.fragment.HomeFragment
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/ll_bg”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@color/color_666666”
android:orientation=“horizontal”>
<fragment
android:id=“@+id/fragmen_home”
android:name=“com.scc.demo.fragment.HomeFragment”
android:layout_width=“0dp”
android:layout_weight=“1”
android:layout_height=“match_parent” />
<fragment
android:id=“@+id/fragmen_mine”
android:name=“com.scc.demo.fragment.MineFragment”
android:layout_width=“0dp”
android:layout_weight=“1”
android:layout_height=“match_parent” />
4.最后在Activity调用setContentView()加载布局文件即可
public class FragmentActivity extends ActivityBase {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MLog.e(this.getClass().getName()+“onCreate”);
setContentView(R.layout.activity_fragment);
}
}
动态创建
-
获得FragmentManager对象,通过getSupportFragmentManager()
-
获得FragmentTransaction对象,通过fm.beginTransaction()
-
调用add()方法或者repalce()方法加载Fragment;
-
最后调用commit()方法提交事务
样例:
1.activity的布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<FrameLayout
android:id=“@+id/fl_frame”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_above=“@+id/rg_bottom_tab”/>
<RadioGroup
android:id=“@+id/rg_bottom_tab”
android:layout_width=“match_parent”
android:layout_height=“56dp”
android:layout_alignParentBottom=“true”
android:background=“#dcdcdc”
android:orientation=“horizontal”>
<RadioButton
android:id=“@+id/rb_home”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:checked=“true”
android:gravity=“center”
android:text=“首页”
android:textSize=“@dimen/text_size_18” />
<RadioButton
android:id=“@+id/rb_list”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:checked=“true”
android:gravity=“center”
android:text=“列表”
android:textSize=“@dimen/text_size_18” />
<RadioButton
android:id=“@+id/rb_news”
android:layout_width=“0dp”
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V:vip204888 备注Android获取(资料价值较高,非无偿)
关于面试的充分准备
一些基础知识和理论肯定是要背的,要理解的背,用自己的语言总结一下背下来。
虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,我能明显感觉到国庆后多了很多高级职位,所以努力让自己成为高级工程师才是最重要的。
好了,希望对大家有所帮助。
接下来是整理的一些Android学习资料,有兴趣的朋友们可以关注下我免费领取方式。
①Android开发核心知识点笔记
②对标“阿里 P7” 40W+年薪企业资深架构师成长学习路线图
③面试精品集锦汇总
④全套体系化高级架构视频
**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水!
ndroid开发核心知识点笔记**
②对标“阿里 P7” 40W+年薪企业资深架构师成长学习路线图
[外链图片转存中…(img-fdjLddvn-1711545154184)]
③面试精品集锦汇总
[外链图片转存中…(img-kiboqXfT-1711545154185)]
④全套体系化高级架构视频
**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水!
[外链图片转存中…(img-YZqZ2qBl-1711545154186)]