Android Floating Action Button doesn't get fixed at the bottom

Deadly 提交于 2020-01-07 03:53:09

问题


I have a fragment wich renders a recycleview with an floating action buttom. The problem is that when the list is empty, the fab keeps on the right top, when the list has few items, the fab keeps bellow the list but not at the bottom, and it only keeps totatly at the bottom when the list fill the screen with items. Could somebody help me please? Bellow is my code:

<?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"
    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.RecyclerView
        android:id="@+id/funcionario_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <android.support.design.widget.FloatingActionButton
        android: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_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior" />


回答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.CoordinatorLayout
    xmlns: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.RecyclerView
        android:id="@+id/funcionario_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <android.support.design.widget.FloatingActionButton
        android: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:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.funcionario_recycler_view);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_func); 

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy)
    {
        if (dy > 0 ||dy<0 && fab.isShown())
        {
            fab.hide();
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState)
    {
        if (newState == RecyclerView.SCROLL_STATE_IDLE)
        {
            fab.show();
        }

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

Hope this will help~



来源:https://stackoverflow.com/questions/44013537/android-floating-action-button-doesnt-get-fixed-at-the-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!