Android 视图绑定:从包含的 xml 布局访问 NavHostFragment 时

时间:2021-02-13 08:24:29

标签: android layout navigation include fragment

我有 activity_main.xml,启用 ViewBinding 可以完美生成 ActivityMainBinding 类。我有另一个 content_main.xml 布局,它包含在这个布局中,如下所示:

activity_main.xml

 <androidx.coordinatorlayout.widget.CoordinatorLayout
        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">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/content_main"
            android:id="@+id/main_container"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

这里是 content_main.xml

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.varshakulkarni.videorecordwithlyricspoc.MainActivity"
    tools:showIn="@layout/activity_main">

    <!--This layout is created to place all the content below the AppBar without overlapping while scrolling.-->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
<!-- Update: replace this with androidx.fragment.app.FragmentContainerView -->
    <fragment
        android:id="@+id/nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity 类中,在访问 TextViews/Buttons 等其他视图时,我们可以使用 binding.mainContainer.textView.text = "test" ,效果很好。

但是我们如何访问androidx.navigation.fragment.NavHostFragment

由于这些片段不是普通视图,因此会出现此错误,“无法访问 'android.widget.fragment'。请检查您的模块类路径中是否存在缺少或冲突的依赖项”

MainActivity.kt

 binding.apply {
            //Works
            mainContainer.textView.text = "test"
            //throws "Cannot access 'android.widget.fragment'. Check your module classpath for missing or conflicting dependencies"
            mainContainer.navHost.postDelayed({
                binding.mainContainer.navHost.systemUiVisibility = FLAGS_FULLSCREEN
            }, IMMERSIVE_FLAG_TIMEOUT)
        }

更新:这在使用 FragmentContainerView 时有效。

2 个答案:

答案 0 :(得分:1)

要托管 navGraph ,您应该使用 FragmentContainerView 而不是 Fragment 并将 name 属性设置为 :

android:name="androidx.navigation.fragment.NavHostFragment"

答案 1 :(得分:0)

使用 findNavController 方法访问导航图。由于您延迟了一些可见性,请尝试使用可运行的方法 Handler().postDelayed

相关问题