文章目录
一、前言
通过上篇可以简单了解Fragment的使用,但是有时候Fragment之间需要传递参数通过构造函数,早些时候都是通过单例来进行传递的,后来官方推出了FragmentFactory
来处理这个事情。本篇不再对之前的方式进行记录,只对FragmentFactory
的使用进行记录
二、创建FragmentFactory
1、修改BlankFragment
class BlankFragment(val content: String) : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("YM","--->获取的内容:$content")
}
}
2、创建BlankFragmentFactory
class BlankFragmentFactory(val content: String): FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
if (className == BlankFragment::class.java.name) {
return BlankFragment(content)
}
return super.instantiate(classLoader, className)
}
}
3、加载BlankFragmentFactory
1、使用FragmentContainerView进行加载
1、修改activity_main.xml
这里需要注意的是android:id
和android:tag
属性。这两个属性必须要有,tag
最好起名为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"
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:id="@+id/container"
android:name="com.hello.world.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="blank_fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2、修改MainActivity.kt
将BlankFragmentFactory
赋值给supportFragmentManager.fragmentFactory
需要在Activity#onCreate()
和Fragment#onCreate()
之前调用
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val fragmentFactory = BlankFragmentFactory("====")
supportFragmentManager.fragmentFactory = fragmentFactory
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
2、动态加载
1、修改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?) {
val fragmentFactory = BlankFragmentFactory("====")
supportFragmentManager.fragmentFactory = fragmentFactory
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction()
.add(R.id.container, BlankFragment::class.java, null)
.commit()
}
}
至此FragmentFactory
的使用方式记录完毕,在Fragment
中添加Fragment
的方式类似,不再记录。