Android 导航体系结构组件-带折叠工具栏的详细视图

Android 导航体系结构组件-带折叠工具栏的详细视图,android,android-architecture-components,android-jetpack,android-architecture-navigation,Android,Android Architecture Components,Android Jetpack,Android Architecture Navigation,在I/O会议上介绍了新导航组件的拟议实践,包括以下模板和拟议理念: 应用程序的一个活动 活动包含工具栏和底部导航栏 一个典型的应用程序通常有一个带有折叠工具栏的详细视图。在这种架构下,人们将如何构建它 是否将工具栏移动到每个XML片段 以编程方式实现折叠工具栏 将细节片段移动到自己的活动中(它可能会使用自己的deeplink)并“打破”理念 试试看 appBarLayout = (AppBarLayout) findViewById(R.id.appbar); if(expandToolb

在I/O会议上介绍了新导航组件的拟议实践,包括以下模板和拟议理念:

  • 应用程序的一个活动
  • 活动包含工具栏和底部导航栏
  • 一个典型的应用程序通常有一个带有折叠工具栏的详细视图。在这种架构下,人们将如何构建它

    • 是否将工具栏移动到每个XML片段
    • 以编程方式实现折叠工具栏
    • 将细节片段移动到自己的活动中(它可能会使用自己的deeplink)并“打破”理念
    试试看

    appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    
    
    if(expandToolbar){
                    appBarLayout.setExpanded(true,true);
                }else{
                    appBarLayout.setExpanded(false,true);
                }
    
    这是一个常用的链接

    也适用于其他人更改工具栏的某些部分 您应该用单独的XML编写自定义工具栏视图,并尝试按语法在详细信息片段中扩展自定义视图,然后隐藏未使用的元素 旧工具栏(如果有)的

    setSupportActionBar(toolbar);
    View logo = getLayoutInflater().inflate(R.layout.view_logo, null);
    toolbar.addView(logo);
    
    下面是如何隐藏不需要的视图

    for (int i = 0; i < toolbar.getChildCount(); ++i) {
            View child = toolbar.getChildAt(i);
    
            // here u can hide all text views for example.
            if (child instanceof TextView) {
                child.setVisibility(View.GONE );
            }
        }
    
    for(int i=0;i

    这种方式比编写两个活动要好得多

    让我们假设

  • 应用程序的一个活动
  • 活动包含工具栏和底部导航栏
  • 应用程序所需的工具栏的所有可能外观都应该在单个工具栏中实现,并且可以是当前活动的片段。 为了不违反依赖倒置原则,所有需要活动工具栏功能的片段都必须实现一个接口。您可以使用OnBackbackChangedListener检查视图的更新

    getFragmentManager().addOnBackStackChangedListener(
        new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                Fragment visibleFragment = ...
                if(visibleFragment instanceof ToolbarControlFragment) {
                    if(visibleFragment.shouldExpandToolbar()) {
                        // set AppBarLayout expanded state
                    }
                    // ...
                }
            }
        }
    );
    
    当一个片段需要一个选项菜单时,您可能还记得这个原则

    我通常建议只使用一个由活动控制的底部导航栏和多个分段工具栏。这降低了复杂性,使应用程序的组件更加独立

    一个典型的应用程序通常有一个带有折叠工具栏的详细视图。在这种架构下,人们将如何构建它

    好问题!我也为此挣扎了一段时间,并得出结论,应该有一个带有NavHostFragment的活动,理想情况下,没有其他活动。这使您能够非常灵活地显示(或不显示)每个屏幕所需的任何内容。重要的是,请确保您的主题删除了ActionBar:

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    
    以编程方式实现折叠工具栏

    这取决于你到底想做什么,但最有可能的是,没有必要这样做。您可以将AppBarLayout、折叠ToolBarLayout和工具栏放入布局中,并像正常情况一样使用它们。为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"/>
    
                ...
    
    
    ...
    
    将细节片段移动到自己的活动中(它可能会使用自己的deeplink)并“打破”理念


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

    我添加了相同的功能,但是有两个工具栏是可用的visible@RahulDevanavar您使用的主题是否禁用了操作栏?如果是这样,我将针对您的具体情况提出另一个问题,并确保包含主题XML。
    <?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"/>
    
                ...