Activity->Activity中动态添加Fragment->add和replace方式添加的区别

XML文件

  • Activity布局文件R.layout.activity_main
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_ll"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/add_child_ll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:id="@+id/replace_child_ll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>
  • 第一次添加的MyFragmentA布局R.layout.inflate_layout_a
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inflate_tv_a"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@android:color/holo_blue_light" />
  • 第二次添加的MyFragmentB布局R.layout.inflate_layout_b
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inflate_tv_b"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@android:color/holo_orange_light"/>

Activtiy代码和Fragment代码

-MyFragmentA代码

kotlin 复制代码
class MyFragmentA : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.inflate_layout_a, container, false)
    }
}

-MyFragmentB代码

kotlin 复制代码
class MyFragmentB : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.inflate_layout_b, container, false)
    }
}
  • Activity代码
kotlin 复制代码
const val TAG = "Yang"
class MainActivity : AppCompatActivity() {
    var addLl : LinearLayout? = null
    var replaceLl : LinearLayout? = null
    var mMainHandler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        addLl = findViewById(R.id.add_child_ll) as? LinearLayout
        replaceLl = findViewById(R.id.replace_child_ll) as? LinearLayout
        mMainHandler.postDelayed({
            val firstFragment1 = MyFragmentA()
            val firstFragment2 = MyFragmentA()
            addLl?.let {
                replaceFragment(firstFragment1, it)
            }
            replaceLl?.let {
                replaceFragment(firstFragment2, it)
            }
        }, 3000)

        mMainHandler.postDelayed({
            val secondFragment1 = MyFragmentB()
            val secondFragment2 = MyFragmentB()
            addLl?.let {
                addFragment(secondFragment1, it)
            }
            replaceLl?.let {
                replaceFragment(secondFragment2, it)
            }
        }, 6000)
    }

    private fun addFragment(fragment: Fragment, targetView: View) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction?.add(targetView.id, fragment)?.commitNowAllowingStateLoss()
    }

    private fun replaceFragment(fragment: Fragment, targetView: View) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction?.replace(targetView.id, fragment)?.commitNowAllowingStateLoss()
    }
}
  • 3s后通过replace()方法在两个LinearLayout中以居中方式添加蓝色背景的MyFragmentAMyFragmentAbackground不会覆盖掉原有Activitybackground
  • 6s后通过add()方法在R.id.add_child_llLinearLayout中以居中方式添加橘色背景的MyFragmentBadd()方法添加并不会移除覆盖掉第一次添加的MyFragmentAbackground。因为不会移除,所以橘色背景的MyFragmentB在第一次添加的MyFragmentAbackground下方继续布局绘制
  • 6s后通过replace()方法在R.id.replace_child_llLinearLayout中以居中方式添加橘色背景的MyFragmentBadd()方法添加会移除覆盖掉第一次添加的MyFragmentAbackground。因为会移除,所以橘色背景的MyFragmentB直接在原有R.id.replace_child_llLinearLayout中直接居中布局绘制
  • 如果只是将新的Fragment添加到Activity,而不关心或者不需要移除已存在的Fragment,使用add()方法。但是如果希望替换container中已存在的Fragment,使用replace()方法

获取当前Fragment的数量

  • 获取ActivityFragment的数量
kotlin 复制代码
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(TAG, "onCreate")
        setContentView(R.layout.activity_main)
        addLl = findViewById(R.id.add_child_ll) as? LinearLayout
        replaceLl = findViewById(R.id.replace_child_ll) as? LinearLayout
        mMainHandler.postDelayed({
            val firstFragment1 = MyFragmentA()
            val firstFragment2 = MyFragmentA()
            addLl?.let {
                replaceFragment(firstFragment1, it)
            }
            replaceLl?.let {
                replaceFragment(firstFragment2, it)
            }
            Log.i(TAG, "3000ms Number of fragments: ${supportFragmentManager.fragments.size}")
            Log.i(TAG, "addLl active fragment count = ${addLl?.let { getActiveFragmentCount(it) }} ")
            Log.i(TAG, "replaceLl active fragment count = ${replaceLl?.let { getActiveFragmentCount(it) }} ")
        }, 3000)

        mMainHandler.postDelayed({
            val secondFragment1 = MyFragmentB()
            val secondFragment2 = MyFragmentB()
            addLl?.let {
                addFragment(secondFragment1, it)
            }
            replaceLl?.let {
                replaceFragment(secondFragment2, it)
            }
            Log.i(TAG, "6000ms Number of fragments: ${supportFragmentManager.fragments.size}")
            Log.i(TAG, "addLl active fragment count = ${addLl?.let { getActiveFragmentCount(it) }} ")
            Log.i(TAG, "replaceLl active fragment count = ${replaceLl?.let { getActiveFragmentCount(it) }} ")
        }, 6000)
    }

// log result
// 3s添加了两个MyFragmentA,数量为2
// 6s添加两个MyFragmentB,移除一个MyFragmentA,数量为3
2024-06-06 11:11:31.427 24538-24538 Yang       I  3000ms Number of fragments: 2
2024-06-06 11:11:34.423 24538-24538 Yang       I  6000ms Number of fragments: 3
  • 获取ViewFragment的数量
kotlin 复制代码
fun getActiveFragmentCount(view : View): Int {
    var count = 0
    supportFragmentManager.fragments.forEach {
        if(it.view?.parent == view){
            count++
        }
    }
    return count
}

// log result
// 3s添加了两个LinearLayout各自添加一个Fragment
2024-06-06 12:57:55.747 24983-24983 Yang        I  3000ms Number of fragments: 2
2024-06-06 12:57:55.747 24983-24983 Yang        I  addLl active fragment count = 1 
2024-06-06 12:57:55.747 24983-24983 Yang        I  replaceLl active fragment count = 1 
// 6s第一个LinearLayout通过add方式添加,
2024-06-06 12:57:58.739 24983-24983 Yang        I  6000ms Number of fragments: 3
2024-06-06 12:57:58.740 24983-24983 Yang        I  addLl active fragment count = 2 
2024-06-06 12:57:58.740 24983-24983 Yang        I  replaceLl active fragment count = 1 

效果图

相关推荐
豆包MarsCode3 分钟前
我用豆包MarsCode IDE 做了一个 CSS 权重小组件
开发语言·前端·javascript·css·ide·html
铅华尽4 分钟前
Java---JDBC案例--手机信息管理系统
java·开发语言·智能手机
X 西安14 分钟前
第十章JavaScript的应用
开发语言·javascript·ecmascript
特种加菲猫18 分钟前
初阶数据结构之队列的实现
开发语言·数据结构·笔记
CV大法好29 分钟前
刘铁猛C#入门 027 抽象和开闭原则
开发语言·c#
七侠镇莫尛貝大侠202334 分钟前
C:mbedtls库实现https双向认证连接示例_七侠镇莫尛貝大侠20241122
c语言·开发语言·https
数据小爬虫@36 分钟前
利用Python爬虫获取淘宝商品评论:实战案例分析
开发语言·爬虫·python
苹果酱05671 小时前
springcloud-网关路由gateway
java·开发语言·spring boot·mysql·中间件
武子康1 小时前
Java-08 深入浅出 MyBatis - 多对多模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据库·sql·mybatis