Android 如何使用折叠工具栏布局处理滚动

Android 如何使用折叠工具栏布局处理滚动,android,material-design,Android,Material Design,我在我的应用程序中添加了折叠工具栏,但现在只有当用户在折叠工具栏布局中滚动图像视图时,它才会折叠。当用户从视图中的任何位置滚动时,如何折叠工具栏 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/re

我在我的应用程序中添加了折叠工具栏,但现在只有当用户在折叠工具栏布局中滚动图像视图时,它才会折叠。当用户从视图中的任何位置滚动时,如何折叠工具栏

这是我的布局

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ProfileActivity">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scaleType="centerCrop"
            android:src="@drawable/default_avatar"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <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/AppTheme.PopupOverlay" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/profile_contents"
    app:layout_anchor="@+id/app_bar"
    app:layout_anchorGravity="bottom"
    android:layout_gravity="bottom"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" />


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

这些是profile_contents.xml的内容,因为我将在将来添加更多项目

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


<TextView
    android:id="@+id/profile_displayName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:text="loading name..."
    android:textColor="@color/black"
    android:textSize="24dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/profile_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:text="loading status..."
    android:textColor="@color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/profile_displayName" />

<Button
    android:id="@+id/profile_send_req_btn"
    android:layout_width="182dp"
    android:layout_height="38dp"
    android:layout_marginTop="44dp"
    android:background="@drawable/profile_button_bg"
    android:padding="10dp"
    android:text="@string/send_friend_request"
    android:textAllCaps="false"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/profile_status" />

</android.support.constraint.ConstraintLayout>

将其添加到
配置文件内容
版面的根视图中

app:layout_behavior="@string/appbar_scrolling_view_behavior"
我们需要定义AppBarLayout和将滚动的视图之间的关联。将
app:layout\u行为添加到RecyclerView或任何其他能够嵌套滚动的视图,如NestedScrollView。支持库包含一个特殊的字符串资源
@string/appbar\u scrolling\u view\u behavior
,它映射到
AppBarLayout.ScrollingViewBehavior
,用于在该特定视图上发生滚动事件时通知
AppBarLayout
。必须在触发事件的视图上建立行为


上面的文本来自这里

将包含的布局放在
嵌套的滚动视图
视图中,因为
约束视图
不是可滚动视图

像这样:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    app:layout_anchor="@+id/app_bar"
    app:layout_anchorGravity="bottom"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/profile_contents"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />

</androidx.core.widget.NestedScrollView>


别忘了将
应用程序:布局\u行为
放在
嵌套滚动视图中

你能把profile\u contents.xml源代码放进去吗?我刚刚更新了我的问题,当我添加这一行时,我包含的布局跳出了布局,并且仍然不会滚动。我很高兴能帮上忙。