Prevent Scrolling FloatingActionButton in ViewPager

I have a ViewPager where one of its pages has a FloatingActionButton. The toolbar uses CoordinatorLayout with scroll|enterAlways, so it disappears when the user scrolls.

This causes the FloatingActionButton to be initially invisible and scroll in view when the user scrolls and the toolbar disappears.

The goal is to keep the FAB fixed on the screen. One of the hacker ideas is to move it to a layout containing ViewPager, but I want it to be on the same page.

Here is the top-level layout:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/new_toolbar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="pin"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:background="@color/primary"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

Quite simply, a toolbar that scrolls out of sight (and returns immediately when scrolling up) and a presentation player.

ViewPager:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_add_black_48dp"
        app:fabSize="normal"
        android:id="@+id/add" />
</FrameLayout>

- , app:layout_scrollFlags="" app:layout_behavior="android.support.design.widget.FloatingActionButton.Behavior", - , , FrameLayout . ?

+4
1

, , , , .

FAB Layout ViewPager, . OnPageChangeListener ViewPager onCreate.

:

fab = (FloatingActionButton) findViewById(R.id.add);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //positions 0, 1, 2 = first tab, second tab, third tab respectively            
        if (position == 0 && positionOffset == 0)
            fab.show();
        else
            fab.hide();
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
0

All Articles