如何折叠AppBarLayout的内容但不折叠Toolbar

7
这是我的应用程序屏幕:

enter image description here

以下是布局xml代码:
<android.support.v4.widget.NestedScrollView
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:title="@string/app_name"
        app:theme="@style/Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/arrow_right_bold"
    app:fabSize="normal" />

我希望在滚动时只折叠唯一的内容。

我发现可以将Toolbar移出AppBarLayout,但是否有更清晰的方法?否则,我将被迫设置工具栏的背景颜色。我对此没有任何问题,但我认为这是某种肮脏的解决方案 :)

所以.如何折叠AppBarLayout的内容而不是Toolbar


这是一篇深入的教程:https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout - gsb
3个回答

6

如果我理解得正确,您想在滚动时折叠标题的一部分。如果是这样,@Nerd的评论链接到了一个很好的教程,告诉您如何做到这一点。无论如何,我将在此处留下一个使用CoordinatorLayout的示例,以实现您所需的效果:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout

            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!-- PUT HERE THE PORTION OF THE HEADER YOU WANT TO COLLAPSE WHEN SCROLLING-->
            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- YOUR SCROLLABLE CONTENT HERE -->
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>

谢谢,但这不是我想要的。我已经更新了相关图片,以使我的需求更加清晰明确。 - Alexandr
1
抱歉,我还是不太明白。使用我的响应代码,如果您将可折叠部分放置在 FrameLayout 中,则可以获得折叠该部分的效果。您可以在其中放置任何所需内容。您期望的行为是否遵循这些模式之一? - Logain
我已经做了,但不是我想要的:https://www.youtube.com/watch?v=nYXWgwO14AQ&feature=youtu.be 这是我的布局xml:http://pastebin.com/eTZYQCiD(AppBarLayout的子项在执行时添加)但是,正如您所看到的,由于它位于AppBarLayout之外,因此没有来自Toolbar的阴影。是否有其他解决方案适用于我的情况? - Alexandr

0

可能我来晚了,但如果你仍然需要处理这个问题,你可以参考这篇文章。

AppBarLayout偏移的威力

他只是把工具栏放在CoordinatorLayout之外。我知道这很激进,但这是我找到的唯一方法。


0

你只需要将你的CoordinatorLayout包裹在LinearLayout中,并将你的工具栏放置在CoordinatorLayout上方:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
  
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    />
  
  <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_start"
    >
    
    <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="300dp"
      >
      
      <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >
       
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          app:layout_collapseMode="parallax"
          >
          
          <!-- Collapsed content -->
          
        </LinearLayout>
      
      </android.support.design.widget.CollapsingToolbarLayout>
      
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      >
      
      <!-- Another content -->
     
    </android.support.v4.widget.NestedScrollView>
    
  </android.support.design.widget.CoordinatorLayout>
  
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接