Skip to content Skip to sidebar Skip to footer

Android Floating Action Button Doesn't Get Fixed At The Bottom

I have a fragment which renders a recycleview with an floating action button. The problem is that when the list is empty, the fab keeps on the right top, when the list has few item

Solution 1:

The problem is your FAB attribute: app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior".

#. You don't need to add this with your FAB. Just remove this attribute and add attribute app:layout_anchor="@id/funcionario_recycler_view" and app:layout_anchorGravity="bottom|right|end" to FAB to anchor it with RecyclerView.

Update your layout as below:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.app.juninho.financeapp.activity.FuncionarioActivity"><android.support.v7.widget.RecyclerViewandroid:id="@+id/funcionario_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/btn_add_func"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|right|end"android:layout_alignParentBottom="true"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_margin="16dp"android:src="@drawable/ic_menu_send"app:layout_anchor="@id/funcionario_recycler_view"app:layout_anchorGravity="bottom|right|end" /></android.support.design.widget.CoordinatorLayout>

#. If you want to hide/show FAB when scrolling RecyclerView, then you can do it pragmatically in your java code as below:

RecyclerViewrecyclerView= (RecyclerView) findViewById(R.id.funcionario_recycler_view);
FloatingActionButtonfab= (FloatingActionButton) findViewById(R.id.btn_add_func); 

recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener()
{
    @OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy)
    {
        if (dy > 0 ||dy<0 && fab.isShown())
        {
            fab.hide();
        }
    }

    @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState)
    {
        if (newState == RecyclerView.SCROLL_STATE_IDLE)
        {
            fab.show();
        }

        super.onScrollStateChanged(recyclerView, newState);
    }
});

Hope this will help~

Post a Comment for "Android Floating Action Button Doesn't Get Fixed At The Bottom"