Android 视图绑定:从包含的 xml 布局访问 NavHostFragment 时
Android View Binding: When accessing NavHostFragment from included xml layout
我有 activity_main.xml,启用 ViewBinding
可以完美生成 ActivityMainBinding
Class。我有另一个 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
class中,在访问TextViews/Buttons等其他视图时,我们可以使用
binding.mainContainer.textView.text = "test"
,效果很好。
但是我们如何访问 androidx.navigation.fragment.NavHostFragment
?
由于片段不是普通视图,出现此错误,“无法访问 'android.widget.fragment'。请检查您的模块 class 路径是否存在缺失或冲突的依赖关系”
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
时有效。
要托管 navGraph ,您应该使用 FragmentContainerView
而不是 Fragment
并将名称属性设置为 :
android:name="androidx.navigation.fragment.NavHostFragment"
我有 activity_main.xml,启用 ViewBinding
可以完美生成 ActivityMainBinding
Class。我有另一个 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
class中,在访问TextViews/Buttons等其他视图时,我们可以使用
binding.mainContainer.textView.text = "test"
,效果很好。
但是我们如何访问 androidx.navigation.fragment.NavHostFragment
?
由于片段不是普通视图,出现此错误,“无法访问 'android.widget.fragment'。请检查您的模块 class 路径是否存在缺失或冲突的依赖关系”
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
时有效。
要托管 navGraph ,您应该使用 FragmentContainerView
而不是 Fragment
并将名称属性设置为 :
android:name="androidx.navigation.fragment.NavHostFragment"