When a FloatingActionButton is anchored to a CollapsingToolbarLayout, it disappears when you scroll up, reappears when you scroll down after a certain point. I was wondering if you can do that with any type of view. In my app, I'm trying to anchor an ImageView to the CollapsingToolbarLayout but it won't disappear like the FloatingActionButton. Here's the XML code.
<android.support.design.widget.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="252dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/random"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/scrollableview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.example.sudarshan.testapp.MLRoundedImageView
android:layout_width="152dp"
android:layout_height="152dp"
app:layout_anchor="@+id/bar"
app:layout_anchorGravity="bottom|center"
android:id="@+id/circularImage"/>
</android.support.design.widget.CoordinatorLayout>
The ImageView gets anchored but it does not disappear and reappear like the FAB does.
Tested and working well
Then call it in xml
Replace yourPackageName with YOUR PACKAGE NAME
Add
app:layout_scrollFlags="scroll|enterAlways"
in your Toolbar xmlCheck here
I hope it's helps you.
This behaviour of disappearing and appearing of View is only associated with
FAB
(FloatingActionButton
). You should have a look on the source code of classFloatingActionButton
. Here is the method inBehavior
class , inner class ofFloatingActionButton
which is responsible for the behaviour.Edit
You can extend the class
FloatingActionButton
to achieve what I think you need.I have extended as follows-
And here is the output -