今天初步接触了一下Fragment(虽然之前也详细学过,但是不知道怎么回事,我写的那篇博客找不到了。算了,无所谓,隔了这么久我也忘得差不多了)做个记录。
这一次只是一个粗略的学习,后面肯定还会系统的总结一下的。
一.Fragment是啥
简单点理解,别想太复杂,Fragment可以理解为一个小型的Activity。就这么简单。
二.为啥要用Fragment?
我们脱离程序,举个形象点的例子。就拿画画来说。比如有一面墙,然后让你画画。你有两种选择。
第一:拿着画笔直接在墙上画。
第二:用一张纸片,纸片用画框框起来,然后挂到墙上在制片上画。你会选择哪一个?(假设墙不能重新涂漆)
OK,我觉得一般人都会选择后者。因为如果直接在墙上画,我们讲过了,墙不能重新涂漆。如果选择前者,如果墙画满了,就要把这个墙拆掉再重新建一个墙再画。这样不仅浪费资源,而且很麻烦。降低了效率。但是如果选择后者就不一样了。如果选择在纸片上画画,等我们画完的时候只需要换一张纸片就行了 ,不仅节约了资源,而且效率也极大的提高了。
那么我们结合程序来说,Activity和Fragment就是第一种选择和第二种选择的关系。如果直接在Activity上面进行操作,当跳转页面的时候,还要重新创建一个Activity。就像是重新建一个墙(因为Activity是比较占内存的)。而如果我们把每一个小页面用Fragment装起来,就类似第二个选择,那就用一个Activity,当需要跳转页面的时候(换一张纸片),直接换个Fragment就行了。非常方面,还节约内存。
三.Fragment和Activity的关系
Fragment依赖于Activity。Fragment就是Activity中不同功能的分离。
因为Activity有自己的生命周期,Fragment依赖于Activity,那么Fragment肯定也有自己的生命周期。在学习Fragment的生命周期之前,我们不妨先复习下Activity的生命周期。
onCreate onStart onResume onPause onStop onDestroy
四.Fragment的生命周期
onAttach:类似于画框添加到(贴)到墙上时,执行
onCreate:创建Fragment(纸片创建成功)
onCreateView:Fragment具体显示的内容。类似于画完画;额
onActivityCreated:Fragment真正的加到Activity上面(画全部挂到墙上了)
onStart:启动Fragment
onResume
onPause
onDestroyView:Fragment依然在内存上面,相当于把纸上的东西擦掉了
onDestroy:Fragment被销毁。相当于纸片被撕掉了
onDetach:相当于画框从墙上拿下来了。
五.Fragment的简单使用(静态的)
所谓静态的,就是写死的。正常情况一般很少这样使用。但是还是要知道,因为这和后面的动态使用有很大关系。
①在Activity对应的xml中添加Fragment控件
注意:name属性一定要有!!!!!别问为啥,就是要有
<fragment
android:id="@+id/mFragment"
android:name="com.example.a20201128.FirstFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
②创建一个类继承于Fragment
class FirstFragment:Fragment() {
}
③重写对应的生命周期方法:注意:onCreateView是必须要写的。因为你要知道它展示啥。其他的看情况
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
④创建与当前Fragment匹配的布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这里是Fragment"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里我是设置了一下Fragment的背景颜色,然后加了个TextView
⑤使用LayoutInflater解析布局文件
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_first,container)
}
展示效果
但是这样没啥意义。我们需要的是灵活的,能够更换的Fragment。所以我们要用动态的方法来使用Fragment
六.Fragment的简单使用(动态的)
步骤大部分和前面静态的差不多。
①在Activity对应的xml中添加FrameLayout容器。这个就类似画框,可以像装着纸片一样装着Fragment
<FrameLayout
android:id="@+id/mContainer"
android:name="com.example.a20201128.FirstFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
②创建一个类继承于Fragment
class SecondFragment : Fragment() {
}
③重写对应的生命周期方法
class SecondFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
}
④创建与当前Fragment匹配的布局文件
<?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=".SecondFragment"
android:background="@color/purple_500">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="SecondFragment "
android:textColor="@color/white"
android:textSize="30sp"/>
</FrameLayout>
⑤使用LayoutInflater解析布局文件
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_second,container,false)
}
⑥使用FragmentManager管理Fragment的切换。
Activity中使用supportFragmentManager获取FragmentManager
supportFragmentManager.beginTransaction()//开启事务
.add(R.id.mContainer,SecondFragment())
.commit()//提交事务
展示效果
OK,刚刚还是有点静态,让我们动起来。
⑦在MainActivity中加几个button,用来控制Fragment的创建和删除
像这样
⑧为他们设置点击事件
mAdd.setOnClickListener {
supportFragmentManager.beginTransaction()
.add(R.id.mContainer,FirstFragment())
.addToBackStack(null)
.commit()
}
mReplace.setOnClickListener {
supportFragmentManager.beginTransaction()
.replace(R.id.mContainer,SecondFragment())
.addToBackStack(null)
.commit()
}
展示效果
当然,还有一些Fragment切换的动画呀还有传输数据这些,还没有说。不慌,我们后面再记录。