CardView 在滚动活动中不会折叠

Cra*_*der 6 java android material-design android-cardview android-coordinatorlayout

我有滚动活动,其中有一个 CardView,其行为类似于此动画中所示的浮动操作按钮。

FAB 隐藏但 CardView 不隐藏

正如您所看到的,fab 正在淡入淡出(进出),但卡片视图的行为并非如此。
我的layout.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true"
    tools:context="com.rhcloud.arshadali.task.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.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" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling" />

    <android.support.v7.widget.CardView
        android:id="@+id/id_card"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        android:foreground="@android:color/white"
        android:layout_margin="5dp">
        <ImageView
            android:id="@+id/app_googleplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxWidth="30dp"
            android:maxHeight="30dp"
            android:layout_gravity="center_vertical"/>
        <TextView
            android:id="@+id/app_apk"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="com.javiersantos"
            android:fontFamily="sans-serif-thin"
            android:textColor="@android:color/black"
            android:gravity="center"/>
    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

有什么方法可以实现这一目标吗?

Sri*_*ddy 0

我认为卡片视图应该是折叠工具栏的子级,以便发生效果(将其放置在工具栏上方)。

<android.support.design.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"
android:fitsSystemWindows="true"
tools:context="com.rhcloud.arshadali.task.ScrollingActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <android.support.v7.widget.CardView
        android:id="@+id/id_card"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        android:foreground="@android:color/white"
        android:layout_margin="5dp">
        <ImageView
            android:id="@+id/app_googleplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxWidth="30dp"
            android:maxHeight="30dp"
            android:layout_gravity="center_vertical"/>
        <TextView
            android:id="@+id/app_apk"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="com.javiersantos"
            android:fontFamily="sans-serif-thin"
            android:textColor="@android:color/black"
            android:gravity="center"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.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" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_scrolling" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email" />
Run Code Online (Sandbox Code Playgroud)