我正在尝试将 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
从我home_fragment.xml
的HomeFragment.kt
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
}
}
底部菜单.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,但现在我点击了菜单项,没有发生任何事情或错误。