【问题标题】:CollapsingToolbarLayout with fixed/pinned Toolbar and functionality of "enterAlways"带有固定/固定工具栏和“enterAlways”功能的 CollapsingToolbarLayout
【发布时间】:2019-12-10 00:35:25
【问题描述】:

我有一个基于material design guidelines 的标准“CollapsingToolbarLayout”实现。

通过以下设置,我能够实现图片上描述的行为:

<CoordinatorLayout ...>
    <AppBarLayout ...>
        <CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|enterAlways"
            ...>
            <Toolbar
                app:layout_collapseMode="pin">
            </Toolbar>
            <MyCustomContent01 ... />
        </CollapsingToolbarLayout>
    </AppBarLayout>
    <MyCustomContent02 ... />
</CoordinatorLayout>

问题

如何实现以下行为?:

  • 向上滚动:始终完全展开工具栏,即使我们不在列表顶部。
  • 向下滚动:只是折叠工具栏,但不要隐藏它。

换句话说:如何在保留第 4 步的条件的同时摆脱第 3 步?

研究

For me the best article on this topic seems to be this one,但是所提供的配置都不符合我的需求。

尝试一:

<CollapsingToolbarLayout
    app:layout_scrollFlags="scroll|enterAlways"
 ...>
  • 永远不要完全隐藏工具栏(摆脱第 3 步):失败
  • 即使我们不在顶部也可以展开工具栏:OK

尝试两次

<CollapsingToolbarLayout
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
 ...>
  • 永远不要完全隐藏工具栏(摆脱第 3 步):确定
  • 即使我们不在顶部,也要展开工具栏:FAIL

【问题讨论】:

  • 即使不在列表顶部,向上滚动时是否要完全展开折叠工具栏布局?
  • 是的。向上滚动:始终完全展开,即使我们不在列表顶部;向下滚动:只是折叠,不要隐藏。

标签: android android-collapsingtoolbarlayout


【解决方案1】:

Material library 提供的默认 CollapsingToolbarLayout 过于局限于一组预定义的动画。要创建我们自己的自定义效果,假设您想在 RecyclerView/ NestedScrollView 上向上滚动时完全展开标题布局,即使您不在滚动视图的顶部,您也可以使用强大的 MotionLayout 它是 ConstraintLayout 的子类用于构建动画。如果您乐于用等效的平面约束布局替换现有的视图层次结构,请阅读下面给出的详细答案。


在这里,我将向您展示如何使用始终固定的标题布局创建“enterAlways”效果,只需三个简单的步骤。

在编写任何代码之前,请查看下面给出的 gif 图像以更好地了解我们要创建的内容。

1.添加ConstraintLayout 依赖:

要在您的项目中使用 MotionLayout,请将 ConstraintLayout 2.0 依赖项添加到您应用的 build.gradle 文件中。如果您使用的是 AndroidX,请添加以下依赖项:

dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
}

如果您不使用 AndroidX,请添加以下支持库依赖项:

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta2'
}

2。创建 MotionLayout 文件:

MotionLayout 是 ConstraintLayout 的子类,因此您可以将任何现有的 ConstraintLayout 转换为 MotionLayout。所以创建一个布局文件,如下所示。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
motion:layoutDescription="@xml/motionscene"
tools:showPaths="false">


<View
    android:id="@+id/toolbar_image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#345634"
    android:fitsSystemWindows="true"
    motion:layout_constraintBottom_toBottomOf="@id/toolbar"
    motion:layout_constraintEnd_toEndOf="parent"
    motion:layout_constraintStart_toStartOf="parent"
    motion:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:src="@drawable/abc_ic_ab_back_material"
    android:tint="?android:attr/textColorPrimaryInverse"
    motion:layout_constraintStart_toStartOf="parent"
    motion:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/customHeader"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    motion:layout_constraintEnd_toEndOf="parent"
    motion:layout_constraintStart_toEndOf="@id/home"
    motion:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/row1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="8dp">

        <View
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="#345678" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="some text"
            android:textColor="#ffffff" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/row2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="8dp">

        <View
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="#345678" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="some text"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/row3"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="8dp">

        <View
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="#345678" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="some text"
            android:textColor="#ffffff" />
    </LinearLayout>


</LinearLayout>


<LinearLayout
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="8dp"
    android:background="#345634"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="8dp"
    motion:layout_constraintEnd_toEndOf="parent"
    motion:layout_constraintStart_toEndOf="@id/home"
    motion:layout_constraintTop_toBottomOf="@id/customHeader">

    <View
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="#F44336" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:text="Toolbar Title"
        android:textColor="#ffffff" />

    <View
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="16dp"
        android:background="#F44336" />
</LinearLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#ffffff"
    motion:layout_constraintBottom_toBottomOf="parent"
    motion:layout_constraintEnd_toEndOf="parent"
    motion:layout_constraintStart_toStartOf="parent"
    motion:layout_constraintTop_toBottomOf="@+id/toolbar_image">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text" />

</androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.motion.widget.MotionLayout>

3.创建 MotionScene:

在上一步中,motion:layoutDescription 属性引用了 MotionScene。 MotionScene 是一个 XML 资源文件,其中包含相应布局的所有运动描述。为了将布局信息与动作描述分开,每个 MotionLayout 都引用一个单独的 MotionScene。请注意,MotionScene 中的定义优先于 MotionLayout 中的任何类似定义。

以下是一个示例 MotionScene 文件,它创建了所需的具有“enterAlways”效果的固定/固定工具栏:

将文件放到res目录下的xml文件夹中。

motionscene.xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">

<Transition
    motion:constraintSetEnd="@id/collapsed"
    motion:constraintSetStart="@id/expanded">

    <OnSwipe
        motion:dragDirection="dragUp"
        motion:moveWhenScrollAtTop="false"
        motion:touchAnchorId="@id/scrollView"
        motion:touchAnchorSide="top" />

</Transition>

<ConstraintSet android:id="@+id/expanded">
    <Constraint android:id="@id/toolbar_image" />
    <Constraint android:id="@id/toolbar" />
    <Constraint android:id="@id/customHeader">
        <PropertySet android:alpha="1" />
    </Constraint>
</ConstraintSet>

<ConstraintSet android:id="@+id/collapsed">
    <Constraint
        android:id="@id/toolbar_image"
        android:layout_height="?attr/actionBarSize"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent">

    </Constraint>
    <Constraint
        android:id="@id/customHeader"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toEndOf="@id/home"
        motion:layout_constraintTop_toTopOf="parent">
        <PropertySet android:alpha="0" />
    </Constraint>

    <Constraint
        android:id="@id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginStart="16dp"
        android:layout_marginTop="0dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toEndOf="@id/home"
        motion:layout_constraintTop_toTopOf="parent">

    </Constraint>

</ConstraintSet>


</MotionScene>

就是这样。您无需编写任何 java/kotlin 代码就创建了一个惊人的自定义动画。 MotionLayout 是完全声明性的,这意味着您可以在 XML 中描述任何转换,无论多么复杂。


Google 的以下 repo 包含更多示例。

https://github.com/googlesamples/android-ConstraintLayoutExamples

【讨论】:

  • 太好了。我什至不知道正在开发像MotionLayout 这样的东西。我找到了一组官方的例子,有人可能会感兴趣:github.com/googlesamples/android-ConstraintLayoutExamples
  • 注意:在您的示例中,您使用了tools:showPaths="true" 属性。这应该设置为 false 用于生产。
  • 注意 2:如果使用最新 (beta2) 版本的 MotionLayout (androidx.constraintlayout:constraintlayout:2.0.0-beta2),您的示例将无法正常工作。工具栏也会折叠直到完全消失:dropbox.com/s/t4vp51e6qo6o8d5/Screenshot_1565251470.png
  • @MichalVician 更新了示例以使用最新版本的 ConstraintLayout。稍微修改了布局和动画,使其更加美观。还附上了更新的gif。谢谢。
【解决方案2】:

你必须结合两个标志来获得这种效果

试试这个

app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"

【讨论】:

  • 不,这不起作用。我想不支持exitUntilCollapsedenterAlways 的组合。我尝试了多个 Android 版本,每个版本的工具栏在第一次向下滚动时都会展开,然后它只是“冻结”——它永远不会倒塌。此外,在工具栏的顶部出现了奇怪的边距。
猜你喜欢
  • 2015-10-02
  • 2017-09-17
  • 2016-08-24
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
相关资源
最近更新 更多