当前位置:   article > 正文

Android中FragmentContainerView的详细介绍

fragmentcontainerview

在Android中,FragmentContainerView是用于容纳和管理Fragment的容器视图。它是一个FrameLayout的子类,提供了一些额外的功能,如支持Navigation库中的NavHostControllerNavController,以及管理Fragment的生命周期和交互。

FragmentContainerView通常用于以下场景:

  1. 作为NavHost的一部分NavHost是一个FrameLayout,它管理着NavController,而NavController又管理着一系列的Fragment

  2. 在Activity中包含Fragment:在Activity的布局中,你可以使用FragmentContainerView来包含和显示Fragment

  3. Fragment中包含另一个Fragment:你可以在一个Fragment中使用FragmentContainerView来包含另一个Fragment

下面是一个简单的例子,展示如何在Activity中使用FragmentContainerView来显示一个Fragment

  1. 创建一个Fragment(例如MyFragment)。

MyFragment.kt:

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController

class MyFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 返回布局视图
        return inflater.inflate(R.layout.fragment_my, container, false)
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

fragment_my.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, Fragment!" />

</FrameLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 在Activity的布局文件中,使用FragmentContainerView来包含MyFragment

activity_main.xml:

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:defaultNavGraph="@navigation/nav_graph" />

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 创建一个导航图nav_graph.xml来定义Fragment之间的导航关系。

nav_graph.xml:

<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/myFragment">

    <fragment
        android:id="@+id/myFragment"
        android:name=".MyFragment"
        android:label="My Fragment" />
</navigation>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 在Activity中获取NavController并使用它来导航到MyFragment

MainActivity.kt:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
        val navController = navHostFragment.navController

        // 使用NavController进行导航
        // 例如,导航到MyFragment
        //
        navController.navigate(R.id.myFragment)
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这个例子中创建了一个名为MyFragment的Fragment,并将其使用FragmentContainerView包含在Activity的布局中。通过创建导航图、获取NavController并调用navigate方法,我们可以在Activity中使用NavController来导航到MyFragment。

此外,需要在项目中添加了Navigation库的依赖项,以便使用NavController和导航功能。要在Activity中使用FragmentContainerView,也需要确保使用了最新版本的AndroidX库和支持库。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/760904
推荐阅读
相关标签
  

闽ICP备14008679号