简体   繁体   English

Android Floating Action Button 没有固定在底部

[英]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 items, the fab keeps below the list but not at the bottom, and it only keeps totally at the bottom when the list fill the screen with items.问题是当列表为空时,fab 保持在右上方,当列表中的项目很少时,fab 保持在列表下方而不是底部,并且只有当列表填满时它才完全保持在底部屏幕与项目。 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" />
</android.support.design.widget.CoordinatorLayout>

if the list is empty, fab keeps at the top, which is wrong:如果列表为空,fab 保持在顶部,这是错误的:
如果列表为空,fab 保持在顶部,这是错误的

if the list has a few items, fab doesn't keep totally at the bottom:如果列表有几个项目,fab 不会完全保留在底部:
如果列表有几个项目,fab 不会完全保持在底部

Here is the correct way, fab is at the bottom:这是正确的方法,fab 在底部:
这是正确的方法,fab 在底部

Using RelativeLayout it seems to work, but its too at the right/bottom corner:使用 RelativeLayout 似乎可以工作,但它也在右/下角:
使用 RelativeLayout 似乎有效,但它也在右/下角

The problem is your FAB attribute: app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior" .问题在于您的FAB属性: app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior"

#. #. You don't need to add this with your FAB .您不需要在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 .只需删除此属性并将属性app:layout_anchor="@id/funcionario_recycler_view"app:layout_anchorGravity="bottom|right|end"FABanchor其与RecyclerView anchor

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时隐藏/显示FAB ,那么你可以在你的java代码中以pragmatically进行,如下所示:

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~希望这会有所帮助~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM