折叠工具栏不固定卡片视图

Collapsing toolbar not pin the card view

我创建了一个 XML 应用栏布局,折叠工具栏布局与 imageview 和嵌套滚动视图,以便预先形成视差滚动。

但是,我想在折叠工具栏中添加一个卡片视图,并使用折叠模式固定它。向上滚动后无法固定。我不知道我做错了什么。有人可以帮忙吗?

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">


    <com.google.android.material.appbar.AppBarLayout

        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF">


        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="pin">

                //textview
            </androidx.cardview.widget.CardView>
            <ImageView
                        android:id="@+id/imageView7"
                        android:layout_width="360dp"
                        android:layout_height="360dp"
                        android:scaleType="centerCrop"
                        app:srcCompat="@drawable/logo"
                        tools:srcCompat="@drawable/logo" />




    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        android:overScrollMode="always"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            //my content
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

据我所知,只有 Toolbar 可以固定在 CollapsingToolbarLayout 中。其他一切都崩溃了。因此,您要么将要固定的布局放在工具栏布局内(不推荐,会更改工具栏的大小和导航按钮的位置等),要么将其移出 CollapsingToolbar。

请记住 AppBarLayout 就像一个垂直的 LinearLayout。

如果您的设计允许,一个可能的想法是删除 CollapsingToolbarLayout 并尝试在 AppBarLayout 中构建您的 UI。您可以使用 layout_scrollFlags.

来控制哪些内容会消失,哪些内容会保留

这是一个非常简单的技巧,您可以应用

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/home_bg_color"
        tools:context=".ui.match_detailds.MatchDetailsActivity">
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/coordinatorLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_barlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:theme="@style/AppTheme.AppBarOverlay">
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collaps_toolabar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
                    <androidx.appcompat.widget.Toolbar
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/_150sdp"
                        app:layout_collapseMode="pin"/>
                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/_150sdp"

                        app:layout_collapseMode="pin">
                    </androidx.constraintlayout.widget.ConstraintLayout>

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/layout_currentMatch_item"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:layout_marginTop="@dimen/_150sdp"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        app:layout_collapseMode="parallax"
                        app:layout_collapseParallaxMultiplier="1">

                    </androidx.constraintlayout.widget.ConstraintLayout>

                </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"
                android:fillViewport="true"
                android:id="@+id/nestedviewhome"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.core.widget.NestedScrollView>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>