文章目录
一、前言
Fragment
早在Android3.0时候就出现了,但是使用方式一直比较复杂。中间经历了很多变化。现在重新将Fragment的使用方式进行整理一下。
二、添加依赖
需要添加以下依赖
implementation 'androidx.fragment:fragment-ktx:1.4.0'
// 或者 不带kotlin的版本
implementation 'androidx.fragment:fragment:1.4.0'
三、使用的两种方式
1、静态使用
1、创建布局文件fragment_blank.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=".BlankFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
2、创建Fragment文件BlankFragment.kt
class BlankFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
}
3、创建activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:name="com.hello.world.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4、创建MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
这样即完成了静态引用的方式
2、动态使用
1、修改activity_main.xml
使用上述已经创建好的布局文件和Fragment文件。更改MainActivit.kt和activity_main.xml为以下方式
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2、修改MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//supportFragmentManager.beginTransaction().add(R.id.container,BlankFragment()).commit()
if(null == savedInstanceState){ //这个可以保证屏幕旋转期间只添加一次Fragment
supportFragmentManager.commit {
setReorderingAllowed(true) //对Fragment的状态进行优化,尤其是执行动画和过渡时候
add(R.id.container,BlankFragment::class.java,null,null)
}
}
}
}
这样即完成了动态加载方式,替换之类这里不进行详细记录