像 ScrollView 中的 FloatingActionButton 一样隐藏自定义视图

Hiding a custom view like the FloatingActionButton does in ScrollView

我正在使用 Android Studio 中提供的示例协调器布局,我将 FloatingActionButton 替换为自定义视图。我注意到当应用程序使用 CollapsingToolbarLayout 向下滚动时 FloatingActionButton 会隐藏,我需要用我的自定义视图复制该行为。

这是布局的 XML:

<?xml version="1.0" encoding="utf-8"?>

<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_model" />

<com.github.lzyzsd.circleprogress.CircleProgress
    android:id="@+id/day_circle"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    custom:circle_prefix_text="$ "
    custom:circle_suffix_text=""/>

这是展开后的样子:

这是折叠时的样子:

您知道如何复制 FloatingActionButton 行为并在工具栏折叠时隐藏自定义视图(圆形进度条)吗?

提前致谢。

public class CircleProgressBehavior extends CoordinatorLayout.Behavior<CircleProgress> {
    public CircleProgressBehavior(Context context, AttributeSet attrs) {
    ...
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, CircleImageView child, View dependency) {
    ...
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, CircleImageView child, View dependency) {
    ...
    }

    @Override
    private void shouldInitProperties(CircleImageView child, View dependency) {
    ...
    }
}

LayoutXML

<com.github.lzyzsd.circleprogress.CircleProgress
    android:id="@+id/day_circle"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:layout_behavior=".CircleProgressBehavior"
    custom:circle_prefix_text="$ "
    custom:circle_suffix_text=""/>

Sample Code 在 Github