I have an activity that has 3 tabs. Each tabs page is a fragment which displays a RecyclerView. One of the fragment has FloatingActionButton in it. I'm implementing this Button in the Fragment's layout. I'm also making it static at the bottom right of the Fragment.
Fragment layout:
- CoordinatorLayout
- RecyclerView
- FAB (without any behavior)
In Activity layout, I have:
- CoordinatorLayout
- AppBarLayout
- Toolbar
- TabLayout (SmartTabLayout)
- ViewPager
The problem now is the FAB is half-hidden from the view when Toolbar is expanded, but fully shown when Toolbar is collapsed. Though this does not happen if I implement the FAB button in Activity itself. But I don't want to have the button in all of the Fragments. I'm only putting it in first Fragment.
Here is a gif I made to explain this clearer.
XML for Activity layout:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/color_primary"
app:layout_scrollFlags="scroll|enterAlways" />
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height"
android:background="@color/color_primary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
XML for Fragment layout:
<?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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_plus_white_48dp"
app:layout_anchor="@id/card_list"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
My question is how do I make so that the button stays visible when recyclerview is scrolled?
To show and hide a FloatingActionButton with the default animation, just call the methods show() and hide() . It's good practice to keep a FloatingActionButton in the Activity layout instead of putting it in a Fragment, this allows the default animations to work when showing and hiding.
The best option would be to just put the FloatingActionButton in the Activity, and call show()
and hide()
in a ViewPager.OnPageChangeListener
. This way you get nice enter/exit animations that conform to Material Design guidelines.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
fabAdd.show();
} else {
fabAdd.hide();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Result:
Add
app:layout_behavior="@string/appbar_scrolling_view_behavior"
to the RecyclerView too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With