如何从片段管理 bottomNavigation?

How to manage bottomNavigation from fragment?

我正在尝试将 navController 设置为 bottomNavigation,但它需要 activity,问题是我的 borromNavigation 托管在片段中,所以我没有 activity 实例。

我试过了

bottom_nav_view.setupWithNavController(findNavController())

和:

val host = Navigation.findNavController(this.activity!!.parent, R.id.my_nav_host_fragment)

bottom_nav_view.setupWithNavController(host)

home_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".ui.fragments.home.HomeFragment">

    <fragment
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/bottom_nav_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/my_nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/main_navigation"
            app:defaultNavHost="true"
            />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/bottom_nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

因此,我需要以某种方式从 HomeFragment.kt

中的 home_fragment.xml 中获取 my_nav_host_fragment

HomeFragment.kt

package andy.schedulekpi.ui.fragments.home

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController

import andy.schedulekpi.R
import andy.schedulekpi.ui.fragments.start.StartFragmentDirections
import kotlinx.android.synthetic.main.home_fragment.*

class HomeFragment : Fragment() {

    companion object {
        fun newInstance() = HomeFragment()
    }

    private lateinit var viewModel: HomeViewModel
    private lateinit var host : NavController

    val safeArgs : HomeFragmentArgs by navArgs()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view =  inflater.inflate(R.layout.home_fragment, container, false)

        //host = Navigation.findNavController(this.activity!!.parent, R.id.my_nav_host_fragment)

        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        // TODO: Use the ViewModel
        Toast.makeText(this.context, safeArgs.currentWeek.toString(), Toast.LENGTH_SHORT).show()

        //val navController = Navigation.findNavController(activity!!.parent, R.id.my_nav_host_fragment) - don't work

        //val host = Navigation.findNavController()  -ERROR

       // bottom_nav_view.setupWithNavController(findNavController()) -ERROR


    }

}

bottom_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:icon="@drawable/ic_schedule"
            android:id="@+id/scheduleFragment"
            android:title="Schedule"/>

    <item
            android:icon="@drawable/ic_database"
            android:id="@+id/archiveFragment"
            android:title="Database"/>
    <item
            android:id="@+id/teachersFragment"
            android:icon="@drawable/ic_teachers"
            android:title="Teachers"/>

    <item
            android:id="@+id/settingsFragment"
            android:icon="@drawable/ic_settings_"
            android:title="Settings"/>

</menu>

main_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/main_navigation"
            app:startDestination="@id/scheduleFragment">
    <fragment
            android:id="@+id/scheduleFragment"
            android:name="andy.schedulekpi.ui.fragments.schedule.ScheduleFragment"
            android:label="schedule_fragment"
            tools:layout="@layout/schedule_fragment"/>
    <fragment
            android:id="@+id/archiveFragment"
            android:name="andy.schedulekpi.ui.fragments.archive.ArchiveFragment"
            android:label="archive_fragment"
            tools:layout="@layout/archive_fragment"/>
    <fragment
            android:id="@+id/teachersFragment"
            android:name="andy.schedulekpi.ui.fragments.teachers.TeachersFragment"
            android:label="teachers_fragment"
            tools:layout="@layout/teachers_fragment"/>
    <fragment
            android:id="@+id/settingsFragment"
            android:name="andy.schedulekpi.ui.fragments.settings.SettingsFragment"
            android:label="settings_fragment"
            tools:layout="@layout/settings_fragment"/>

</navigation>

我希望从片段中处理 bottomNavigation,但现在我点击了菜单项,但没有任何反应或错误。

在 HomeFragment 中设置 BottomNavigationView 上的 navController:

import androidx.navigation.findNavController
Navigation.setViewNavController(bottom_nav_view, activity!!.findNavController(R.id.my_nav_host_fragment))

然后当你想从 bottom_nav_view 获取 navController 时:

bottom_nav_view.findNavController()

如前所述Artur Gniewowski,在onActivityCreated的HomeFragment中我只需要添加

bottom_nav_view.setupWithNavController(activity!!.findNavController(R.id.my_nav_host_fragment))

所以,现在在 HomeFragment 中我有:

package andy.schedulekpi.ui.fragments.home

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.navigation.fragment.navArgs
import androidx.navigation.ui.setupWithNavController

import andy.schedulekpi.R
import kotlinx.android.synthetic.main.home_fragment.*

import kotlinx.android.synthetic.main.home_fragment.view.*

class HomeFragment : Fragment() {

    companion object {
        fun newInstance() = HomeFragment()
    }

    private lateinit var viewModel: HomeViewModel


    val safeArgs : HomeFragmentArgs by navArgs()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view =  inflater.inflate(R.layout.home_fragment, container, false)


        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        // TODO: Use the ViewModel
        //Navigation.setViewNavController(bottom_nav_view, activity!!.findNavController(R.id.my_nav_host_fragment))
        bottom_nav_view.setupWithNavController(activity!!.findNavController(R.id.my_nav_host_fragment))

        Toast.makeText(this.context, safeArgs.currentWeek.toString(), Toast.LENGTH_SHORT).show()
    }

}

感谢Artur Gniewowski