Android:导航组件-如何将工具栏菜单连接到片段?

时间:2020-09-06 08:33:57

标签: android kotlin android-fragments android-jetpack android-architecture-navigation

我被困在尝试将导航组件添加到我的应用中。专门针对我的工具栏菜单项。

我一直在关注Android网站上的navigation migrate文档,但是我对它的工作方式甚至设置感到困惑。

这是一个单活动多片段架构。

该应用程序将从MainFragment开始,然后在工具栏上点击菜单项时,例如从Main Frag到Search Movie Frag。用户将能够从应用程序中的任何位置使用菜单项进行导航(例如,如果单击主页图标,则应将其发送到应用程序中任何位置的主屏幕表单。仍在考虑是否应该这样做)。

主要是我不知道如何正确地将导航组件附加到菜单项。

应用

Main Fragment displayed on phone

导航图

Nav Graph displayed on Android Studio

nav_graph.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/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.movieapp.ui.main.MainFragment"
        android:label="MainFragment">
        <action
            android:id="@+id/action_mainFragment_to_searchMovieFragment"
            app:destination="@id/searchMovieFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchMovieFragment"
        android:name="com.example.movieapp.ui.search.SearchMovieFragment"
        android:label="SearchMovieFragment" />
</navigation>

main_activity.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <include
        layout="@layout/appbar"
        android:id="@+id/mainToolBar"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/mainToolBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var toolbar: Toolbar
    private val navController by lazy {
        (supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController
    }
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        toolbar = findViewById(R.id.mainToolBar)
        setSupportActionBar(toolbar)
        appBarConfiguration = AppBarConfiguration(navController.graph, null)
        setupActionBarWithNavController(navController, appBarConfiguration)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.main_menu_bar, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

MainFragment.kt

class MainFragment : Fragment() {

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

    private lateinit var viewModel: MainViewModel
    private lateinit var binding: MainFragmentBinding
    private lateinit var navController: NavController

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment, container, false)
        setHasOptionsMenu(true)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        navController = Navigation.findNavController(view)
//        navController.navigate(R.id.action_mainFragment_to_searchMovieFragment)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        binding.viewModel = viewModel
        // TODO: Use the ViewModel
    }

}

main_fragment.xml

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

    <data>
        <variable
            name="viewModel"
            type="com.example.movieapp.ui.main.MainViewModel" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.main.MainFragment">

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MainFragment1" />

    </LinearLayout>
</layout>

2 个答案:

答案 0 :(得分:2)

您可以在最基本的FragmentContainerView顶部添加工具栏,从其他片段访问此工具栏,并根据其中的片段更改我单独创建的工具栏,同时按如下所示打开相关片段。

        toolbar = requireActivity().findViewById(R.id.toolbar_base)

        toolbar?.menu?.clear()
        toolbar?.title = ""
        val toolbarFlowFragment = context?.let { getInflateLayout(it, R.layout.toolbar_profile) }

        toolbar?.addView(toolbarFlowFragment)

答案 1 :(得分:1)

文件main_menu_bar.xml中菜单项的ID必须与导航图(nav_graph.xml)中指定的目的地(片段)的ID相匹配。

您的main_menu_bar.xml应该看起来像这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@id/searchMovieFragment"
            android:icon="@drawable/search"
            android:title="@string/menu_search" />
        <item
            android:id="@id/mainFragment"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
    </group>
</menu>

此外,您需要覆盖onOptionsItemSelected(MenuItem)方法并将菜单项与目标关联。像这样:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    val navController = findNavController(R.id.nav_host_fragment)
    return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}

Reference