Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a custom view like the FloatingActionButton does in ScrollView

I'm using the example coordinator layour provided in Android Studio and I replaced the FloatingActionButton for a custom view. I've noticed that the FloatingActionButton hides when the app scrolls down using a CollapsingToolbarLayout and I need to replicate that behavior with my custom view.

Here is The XML of the Layout:

<?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=""/>

This is How it looks when it's expanded:

enter image description here

And this is how it looks when it's collapsed:

enter image description here

Do you have any idea how can I replicate the FloatingActionButton behavior and hide the custom view (Circular progress bar) when the toolbar collapses?

Thank you in advance.

like image 655
chntgomez Avatar asked Dec 04 '25 09:12

chntgomez


1 Answers

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) {
    ...
    }
}

The XML of Layout

<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 on Github

like image 162
Jones.Lee Avatar answered Dec 07 '25 00:12

Jones.Lee