Android浮动操作按钮未固定在底部 [英] Android Floating Action Button doesn't get fixed at the bottom

查看:136
本文介绍了Android浮动操作按钮未固定在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,其中使用浮动动作按钮来渲染一个recycleview.问题在于,当列表为空时,晶圆厂将保持在右上方,当列表中的物品较少时,晶圆厂将保持在该列表的下方,而不是在底部,并且当列表填充时,该晶圆厂将一直保持在底部.屏幕上的项目.有人可以帮我吗?贝娄是我的代码:

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

推荐答案

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

#..您无需在FAB中添加它.只需删除此属性,然后使用RecyclerView将属性app:layout_anchor="@id/funcionario_recycler_view"app:layout_anchorGravity="bottom|right|end"添加到FABanchor.

#. 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.

如下更新您的布局:

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

#..如果要在滚动RecyclerView时隐藏/显示FAB,则可以在Java代码中执行pragmatically,如下所示:

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

希望这会有所帮助〜

这篇关于Android浮动操作按钮未固定在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆