Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/Hide FloatingActionButton on ViewPager swipe

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.

Fab hidden

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?

like image 481
emen Avatar asked Feb 04 '16 07:02

emen


People also ask

How to Hide fab button in android?

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.


2 Answers

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:

enter image description here

like image 72
Daniel Nugent Avatar answered Oct 01 '22 10:10

Daniel Nugent


Add

app:layout_behavior="@string/appbar_scrolling_view_behavior"

to the RecyclerView too.

like image 28
GPack Avatar answered Oct 01 '22 08:10

GPack