Jetpack 导航:操作栏中的标题和 back/up 箭头?

Jetpack navigation: Title and back/up arrow in the action bar?

我已经安装了最新的金丝雀版 Android Studio,并按照此 (https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing) 说明实现了一个简单的两页导航。基本上 page1 有一个按钮,当它被点击时,应用程序显示 page2.

它可以工作,但有一个问题...它似乎没有自动对操作栏执行任何操作。导航库是否应该在操作栏上自动显示 up/back 箭头和 "Label" 属性?还是我应该像以前一样手动完成所有工作?我想在显示第 2 页时在操作(工具)栏上显示后退箭头和 "Details"。

在第 1 页点击按钮。

override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
    button1.setOnClickListener {
        val nav = NavHostFragment.findNavController(this);
        nav.navigate(R.id.show_page2)
    }
}

主要activityXML。默认情况下它是默认的 Action Bar,我用 ToolBar 替换了它。没有区别。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent">
    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/nav_graph"/>

</androidx.constraintlayout.widget.ConstraintLayout>

导航图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/page1">

    <activity
        android:id="@+id/mainActivity2"
        android:name="com.android.navtest.MainActivity"
        android:label="activity_main"
        tools:layout="@layout/activity_main"/>
    <fragment
        android:id="@+id/page1"
        android:name="com.android.navtest.BlankFragment2"
        android:label="Home page"
        tools:layout="@layout/page1">
        <action
            android:id="@+id/show_page2"
            app:destination="@id/page2"
            app:enterAnim="@anim/anim1"
            app:popExitAnim="@anim/anim2"/>
    </fragment>
    <fragment
        android:id="@+id/page2"
        android:name="com.android.navtest.BlankFragment"
        android:label="Details"
        tools:layout="@layout/page2"/>
</navigation>

您可以使用 NavigationUI.setupActionBarWithNavController() 将 ActionBar 连接到 NavController。这通常在您调用 setSupportActionBar():

后立即在您的 Activity 中完成
supportActionBar = findViewById<Toolbar>(R.id.toolbar)

// Get the NavController for your NavHostFragment
val navController = findNavController(R.id.nav_host_fragment)

// Set up the ActionBar to stay in sync with the NavController
setupActionBarWithNavController(navController)

此方法包含在 Navigation talk at Google I/O 2018 中。

如果你想将导航后退按钮隐藏在多个地方(默认仅用于主页片段),你可以将片段的 id 添加到 AppBarConfiguration 并将其作为 setupActionBarWithNavController 的第二个参数传递,例如:

val appBarConfiguration = AppBarConfiguration(setOf(R.id.splashFragment, R.id.onboardingFragment, R.id.homeFragment))

setupActionBarWithNavController(findNavController(R.id.nav_host), appBarConfiguration)

这就是我所做的。
onSupportNavigateUp 当用户向上导航并再次设置时调用。

通过调用此 setupActionBarWithNavController 告诉 android 更新标题工具栏。

navigateUp 通过将其行为委托给给定的 NavController 来处理向上按钮。这通常应该从 AppCompatActivity.onSupportNavigateUp().

调用
private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityGameConfigBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_game_config)
        supportActionBar?.show()

        val navController = Navigation.findNavController(this, R.id.myNavHostFragment)
        NavigationUI.setupActionBarWithNavController(this, navController, null)
        appBarConfiguration = AppBarConfiguration.Builder(navController.graph)
            .build()

        NavigationUI.setupWithNavController(binding.navView, navController)
    }
    override fun onSupportNavigateUp(): Boolean {
        val navController = Navigation.findNavController(this, R.id.myNavHostFragment)
        return NavigationUI.navigateUp(navController, appBarConfiguration)
    }

我的绑定解决方案 - 代码在 MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = DataBindingUtil.setContentView(this, R.layout.main_activity)
    navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    navController = navHostFragment.navController

    setSupportActionBar(toolbar)//needs to be after binding
    toolbar.setupWithNavController(navController,AppBarConfiguration(navController.graph))
}

至于标题 - 首先,我从导航图中的片段中删除了标签 (android:label)(标签覆盖了我测试过的标题)

    <fragment
        android:id="@+id/productListFragment"
        android:name="com.example.ProductListFragment"
        android:label="TO_BE_REMOVED"
        tools:layout="@layout/product_list_fragment">
        <action
            android:id="@+id/action_productListFragment_to_mainMenuFragment"
            app:destination="@id/mainMenuFragment" />
    </fragment>

每个片段在 onResume 中设置标题和副标题,此处示例来自 ProductListFragment

override fun onResume() {
    super.onResume()
    val actionBar = (activity as AppCompatActivity).supportActionBar
    actionBar?.title = getString(R.string.product_list_title)
    actionBar?.subtitle = getString(R.string.product_list_subtitle)
}