如何从片段管理bottomNavigation?

时间:2019-08-01 23:10:51

标签: android android-fragments bottomnavigationview android-jetpack-navigation

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

我尝试过

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>

因此,我需要以某种方式从my_nav_host_fragment的{​​{1}}中提取home_fragment.xml

HomeFragment.kt

HomeFragment.kt

bottom_menu.xml:

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


    }

}

main_navigation.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>

我原本希望处理片段中的bottomNavigation,但是现在我单击了菜单项,没有任何反应或出错。

2 个答案:

答案 0 :(得分:1)

在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()

答案 1 :(得分:0)

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