【发布时间】:2021-10-14 02:50:45
【问题描述】:
在我的 fragment_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph='@navigation/bottom_bar_nav_graph' />
<!--custom bottom navigation ui-->
</androidx.constraintlayout.widget.ConstraintLayout>
我想从片段访问navController(***bottom_bar_nav_graph***)。
有可能吗?
【问题讨论】:
-
那么,您有一个
BottomNavigationView,并且您想从这个 navView 片段之一访问 navController?还是必须通过 navGraph 访问它? -
必须通过navGraph访问它。因为我使用的是 SmoothBottomBar 库 (github.com/ibrahimsn98/SmoothBottomBar),它不是真正的 BottomNavigationView!
-
findNavController()不起作用? -
很遗憾没有! @SamChen
标签: android kotlin fragment android-architecture-components android-navigation