Android 折叠工具栏带有固定/固定工具栏的布局和功能;“永远都是”;
我有一个基于的标准“CollasingToolbarLayout”实现 通过以下设置,我能够实现图片上描述的行为:Android 折叠工具栏带有固定/固定工具栏的布局和功能;“永远都是”;,android,android-collapsingtoolbarlayout,Android,Android Collapsingtoolbarlayout,我有一个基于的标准“CollasingToolbarLayout”实现 通过以下设置,我能够实现图片上描述的行为: <CoordinatorLayout ...> <AppBarLayout ...> <CollapsingToolbarLayout app:layout_scrollFlags="scroll|enterAlways" ...> <Toolb
<CoordinatorLayout ...>
<AppBarLayout ...>
<CollapsingToolbarLayout
app:layout_scrollFlags="scroll|enterAlways"
...>
<Toolbar
app:layout_collapseMode="pin">
</Toolbar>
<MyCustomContent01 ... />
</CollapsingToolbarLayout>
</AppBarLayout>
<MyCustomContent02 ... />
</CoordinatorLayout>
问题:
如何实现以下行为?:
- 向上滚动:始终完全展开工具栏,即使我们不在列表顶部
- 向下滚动:只需折叠工具栏,但不要隐藏它
换句话说:我如何在保留步骤4的条件的同时摆脱步骤3
研究
但是,所提供的配置都不符合我的需要
尝试一次:
<CollapsingToolbarLayout
app:layout_scrollFlags="scroll|enterAlways"
...>
- 永远不要完全隐藏工具栏(摆脱步骤3):FAIL
- 即使我们不在顶部,也要展开工具栏:OK
尝试两次
<CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed"
...>
- 永远不要完全隐藏工具栏(摆脱步骤3):OK
- 即使我们不在顶部,也要展开工具栏:FAIL
您必须将两个标志组合起来才能获得这种效果
试试这个
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"
材质库提供的默认折叠工具栏布局过于局限于一组预定义动画。要创建我们自己的自定义效果,假设您希望在RecyclerView/NestedScrollView上向上滚动时完全扩展标题布局,即使您不在滚动视图的顶部,也可以使用ConstraintLayout的子类powerful来构建动画。如果愿意使用等效平面约束布局替换现有视图层次,请阅读下面给出的详细答案
在这里,我将向您展示如何使用始终固定的标题布局创建“enterAlways”效果,所有这一切只需三个简单的步骤
在编写任何代码之前,请参阅下面给出的gif图像,以便更好地理解我们试图创建的内容
1。添加ConstraintLayout
依赖项:
要在项目中使用MotionLayout,请将ConstraintLayout 2.0依赖项添加到应用程序的build.gradle文件中。如果您使用的是AndroidX,请添加以下依赖项:
dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
}
如果未使用AndroidX,请添加以下支持库依赖项:
dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
}
2。创建MotionLayout文件:
<CollapsingToolbarLayout
app:layout_scrollFlags="scroll|enterAlways"
...>
MotionLayout是ConstraintLayout的子类,因此可以将任何现有ConstraintLayout转换为MotionLayout。因此,创建一个布局文件,如下所示
activity_main.xml
即使不在列表顶部,也要在向上滚动时完全展开折叠工具栏布局吗?是。向上滚动:始终完全展开,即使我们不在列表的顶部;向下滚动:只是折叠,不要隐藏。不,这不起作用。我认为不支持将exituntilclopsed
和enterally
组合使用。我尝试了多个Android版本,在每一个版本上,当第一次向下滚动时,工具栏就会展开,然后它就会“冻结”——它永远不会向后折叠。此外,在工具栏的顶部会出现奇怪的边距。太好了。我甚至不知道像MotionLayout
这样的东西正在开发中。我找到了一组官方示例,可能有人对此感兴趣:注意:在您的示例中,您使用了工具:showpath=“true”
属性。对于生产,应将其设置为false
。注意2:如果使用最新(beta2)版本的MotionLayout
(androidx.constraintlayout:constraintlayout:2.0.0-beta2
)。工具栏也会折叠,直到完全消失:@MichalVician更新了示例以使用最新版本的ConstraintLayout。稍微修改了布局和动画,使其更加美观。还附上了更新的gif。非常感谢。
<?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>