Android浮动操作按钮未在底部修复

时间:2017-05-17 00:14:46

标签: android android-layout android-recyclerview floating-action-button

我有一个片段,它呈现了一个带有浮动动作的循环视图。问题是当列表为空时,工厂保持在右上方,当列表中的项目很少时,工厂保持在列表中但不在列表底部,并且当列表填充时,它只保持在底部屏幕与项目。有人可以帮帮我吗?贝娄是我的代码:

<?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" />

if the list is empty, fab keeps at the top, wich is wrong if the list has a few items, fab doesn't keep totaly at the bottom

Here is the correct way, fab is at the bottom

Using RelativeLayout it seems to work, but its too at the right/bottom corner

1 个答案:

答案 0 :(得分:0)

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

#。您无需在FAB中添加此内容。只需删除此属性,然后将app:layout_anchor="@id/funcionario_recycler_view"app:layout_anchorGravity="bottom|right|end"添加到FAB anchor RecyclerView

更新您的布局,如下所示:

<?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>

#。如果您想在滚动FAB时隐藏/显示RecyclerView,那么您可以在java代码中执行pragmatically,如下所示:< / p>

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);
    }
});

希望这会有所帮助〜