android - 导航架构组件 - 带有 CollapsingToolbar 的详细 View

标签 android android-architecture-components android-jetpack android-architecture-navigation

新导航组件的提议实践在 I/O 上展示了以下模板和提议的理念:

  1. 一个应用的一个 Activity
  2. Activity 包含工具栏和底部导航栏

一个典型的应用程序通常有一个带有 CollapsingToolbar 的详细 View 。在那种架构下如何构建它?

  • 将工具栏移动到每个 fragment XML?
  • 以编程方式实现折叠工具栏?
  • 将细节 fragment 移动到它自己的 Activity 中(无论如何它可能会使用自己的深层链接)并“打破”理念?

最佳答案

A typical app often has a detail view with a CollapsingToolbar in it. How would one build that under that architecture?

好问题!我也为此苦苦挣扎了一下,得出的结论是应该有一个带有 NavHostFragment 的 Activity,理想情况下,没有别的。这为您提供了最大的灵 active 来显示(或不显示)每个屏幕所需的任何内容。重要的是,请确保您的主题删除了 ActionBar:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

这会引出你的下一个问题...

Move Toolbar to each Fragment XML?

在我看来,是的!您通常使用 ActionBar 的一切都可以通过工具栏完成。下面是一个简短的 fragment ,展示了如何使用工具栏来完成您过去使用 ActionBar 的最重要的事情(向上导航、标题、选项菜单等...):

toolbar.apply {
    setNavigationOnClickListener { findNavController().navigateUp() }
    setTitle(R.string.toolbar_title)
    inflateMenu(R.menu.fragment_menu)
    setOnMenuItemClickListener(::onMenuItemClick)
}

Implement the collapsing toolbar programmatically?

这取决于您到底想做什么,但很可能没有必要这样做。您可以将 AppBarLayout、CollapsingToolbarLayout 和 Toolbar 拖放到您的布局中并像平常一样使用它们。给你的 AppBarLayout 一个 ActionBar 主题覆盖。这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="@color/primary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:navigationIcon="@drawable/ic_up_white"/>

            ...

Move the detail fragment to its own activity (it may use its own deeplink anyway) and 'break' the philosophy?

上面不需要那个,对吧?这种方法足够灵活,可以在一个导航图中轻松容纳多个级别,并且仍然能够自定义图中每个目的地的外观和行为(包括类似于 ActionBar 的功能)。

关于android - 导航架构组件 - 带有 CollapsingToolbar 的详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50320410/

相关文章:

android - minifiedEnabled 导致渲染脚本崩溃 >Kitkat

java - 错误 :Execution failed for task ':app:transformClassesWithMultidexlistForDebug'

java - 如何将 int[] 转换为 Integer[]?

android-studio - 缺少 Activity & Fragment+ViewModel 模板

java - 从sqlite中获取数据并存储在json中

android - 实时数据+ View 模型+房间: Exposing a LiveData returned by query which changes over time (Through a fts search)

java - ViewModel 没有零参数构造函数错误,原因是 ViewModelFactory 一直为 null(不是 100% 确定)

android - 如何使用导航 Controller 组件设置不同的工具栏?

android - 如何在使用之前更改传入的修饰符?

android - 拦截 NavigationUI.onNavDestinationSelected() 使 backstack 弹出 "inclusive = true"