NavigationRailView + fragment + Navigation

有一種場景,NavigationRailView + fragment + Navigation ,通過 findNavController().navigate() 跳轉(zhuǎn)至DetailFragment,點擊NavigationRailView 其他標簽,再點回上一次的標簽,此時標簽未被選中,顯示的頁面為之前操作跳轉(zhuǎn)的DetailFragment。必須在DetailFragment 中 調(diào)用 findNavController().popBackStack() 返回,標簽才會被選中。如何在NavigationRailView 標簽點擊時,每次都顯示當前標簽下的首頁fragment???

2022-12-15 更新

通過反復試驗 可以使用 findNavController().clearBackStack() 在切換新的fragment 里 將之前的fragment 清除

override fun onAttach(context: Context) {
        super.onAttach(context)
        findNavController().clearBackStack(R.id.VideoFragment)
    }
原理待分析,暫且解決了我的問題吧。。。

2023-04-23 再次更新,終極解決方案;

借助ChatGpt的幫助:NavigationRailView 與 NavHost 組件進行導航,即 每個tab標簽對應一組NavHost。以兩個tab為例具體步驟:

1.創(chuàng)建多個 NavGraph

在項目中創(chuàng)建多個 NavGraph,每個 NavGraph 代表應用程序的不同區(qū)域或不同的功能模塊。我們可以使用 navigation 組件在 res 目錄下的 navigation 文件夾中創(chuàng)建每個 NavGraph。

2.在 XML 布局文件中添加

NavigationRailView
在主 XML 布局文件中添加 NavigationRailView,并將其與 NavHost 組件進行關聯(lián)。以下是一個示例 XML 布局文件:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.navigation.NavigationRailView
        android:id="@+id/navigation_rail"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:background="#fff"
        app:menu="@menu/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:itemIconPadding="12dp"
        app:itemTextAppearance="@style/TextAppearance.AppCompat.Body2"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:selectedItem="@id/home" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/home_nav"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/navigation_rail"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

3.創(chuàng)建 menu 資源文件

我們需要在 res/menu 目錄下創(chuàng)建一個 menu 資源文件,并將其與 NavigationRailView 中的 app:menu 屬性關聯(lián)。在該文件中,我們可以添加菜單項,這些菜單項將用于切換 NavHost 中的不同 NavGraph。以下是一個示例 menu 文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/home"
            android:icon="@drawable/ic_home"
            android:title="Home" />
        <item
            android:id="@+id/settings"
            android:icon="@drawable/ic_settings"
            android:title="Settings" />
    </group>
</menu>

此菜單將在 NavigationRailView 中顯示兩個菜單項,分別用于切換到 home_nav 和 settings_nav。

4.在 Activity 或 Fragment 中實現(xiàn)

NavigationRailView.OnItemSelectedListener
我們需要在 Activity 或 Fragment 中實現(xiàn) NavigationRailView.OnItemSelectedListener 接口。用戶單擊 NavigationRailView 中的菜單項時調(diào)用。在 onNavigationItemSelected 方法中,我們需要根據(jù)用戶單擊的菜單項 ID 加載相應的 NavGraph。核心代碼:

..........
   override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val navController = nav_host_fragment.findNavController()
        when (item.itemId) {
            R.id.home -> {
                navController.setGraph(R.navigation.home_nav)
                return true
            }
            R.id.settings -> {
                navController.setGraph(R.navigation.settings_nav)
                return true
            }
            else -> return false
        }
    }
}
.......

在 onNavigationItemSelected 方法中,我們獲取當前 NavHost 中的 NavController 對象,并根據(jù)用戶單擊的菜單項 ID 加載相應的 NavGraph。每一次切換相當于重新設置新的路由,這樣不管在那個標簽下進行跳轉(zhuǎn),再切換標簽,之前的fragment都會出棧。保證每一次切換都回到當前標簽下的首頁。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容