防止浮动操作按钮在折叠工具栏布局上消失



我有一个详细信息片段,显示有关所选项目的信息。图像嵌入在CoordinatorLayoutCollapsingToolbarLayout中。该行为按预期工作:向下滚动图像时,FloatingActionButton会随着CollapsingToolbarLayout的末尾向上滚动。然后,当它完全折叠时,FloatingActionButton消失了。我想发生的是这个按钮永远不会消失。它锚定在工具栏上很好,但是在查看文档和此站点后,我一直无法找到如何保持可见。任何提示将不胜感激!

这是我布局的简化版本:

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
app:contentScrim="@color/colorPrimaryDark"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="@{movie.title}"
app:toolbarId="@+id/toolbar">
<ImageView
android:id="@+id/fragment_details_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/browse_image_content_description"
android:scaleType="centerCrop"
app:largeSize="@{true}"
app:layout_collapseMode="parallax"
app:posterPath="@{movie.posterPath}" />
<androidx.appcompat.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" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.heiligbasil.movietvdelight.view.DetailsFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/fragment_details_text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@{movie.title}"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="6dp"
app:borderWidth="0dp"
app:fabSize="mini"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:pressedTranslationZ="10dp"
app:rippleColor="@color/colorPrimary"
app:srcCompat="@drawable/ic_save" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我想发生的是这个按钮永远不会消失

这实际上是因为app:layout_anchor="@id/app_bar"您的FloatingActionButton具有特殊的关系,并通过滚动时AppBarLayout进行控制。

要禁用此功能,请设置:app:behavior_autoHide="false"到您的FloatingActionButton


此外,您可以使用setAutoHideEnabled()以编程方式执行此操作。这正是您要查找的:https://stackoverflow.com/a/43077760/4409113

最新更新