防止FloatingActionButton在CollapsingToolbarLayout上消失

3
我有一个详情片段,显示所选项目的信息。图像嵌入在一个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>
1个回答

2

3
XML 属性对行为没有任何影响,但是编程解决方案却奏效了!val layoutParams = binding.fab.layoutParams as CoordinatorLayout.LayoutParams var fabBehavior = layoutParams.behavior as? FloatingActionButton.Behavior fabBehavior?.let { it.isAutoHideEnabled = false } ?: run { fabBehavior = FloatingActionButton.Behavior() fabBehavior?.isAutoHideEnabled = false layoutParams.behavior = fabBehavior } - heiligbasil

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