带有FloatingActionButton的RecyclerView

20

我有一个活动,其中包含RecyclerView(可选为CardView)和FloatingActionButton

我希望FAB始终在屏幕右下方,但当我滚动到RecyclerView的末尾时,FAB会遮挡最后一个项目的部分内容。

在父CardView(或删除CardView后的RecyclerView本身)上使用android:layout_marginBottom="48dp"可以解决此问题,但会导致RecyclerView缩小到屏幕大小减去48dp的边距。

我想让RecyclerView全尺寸(即适合所有项目),但当我滚动到最后一个项目时,应该有那个边距,以使FAB不会覆盖RecyclerView的最后一个项目。这类似于Google Inbox / Gmail应用程序中电子邮件列表的行为。

我已经看到了许多类似(但不同)问题的问题,但没有任何解决方案适用于我。我知道应该有一些简单的方法来解决这个问题,我不想扩展LinearLayoutManager,因为这似乎对于这个问题来说太多了。我也不想在滚动时隐藏FAB。

以下是我目前的内容:

activity_main.xml

<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"
    android:background="@color/colorBackground"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/card_list"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:onClick="onClick"
        app:srcCompat="@drawable/ic_add"/>
</android.support.design.widget.CoordinatorLayout>

card_list.xml

<android.support.v7.widget.CardView
    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"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="@android:color/white"
    android:layout_marginBottom="48dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.v7.widget.CardView>
1个回答

31

移除CardView中的layout_marginBottom,并将以下内容添加到RecyclerView中:

android:paddingBottom="48dp"
android:clipToPadding="false"

实际上,我正在使用 layout_marginBottompaddingBottom 是其中一次尝试的结果。已更新问题。尝试了您的建议,包括和不包括 layout_marginBottom,但仅在 RecyclerView 的末尾添加了额外的空白空间。 - A.A.
很抱歉@A.A.,它是clipToPadding="false"(而不是"true")。我已经编辑了我的答案。 - mVck
非常接近我想要的了,但列表末尾仍有空白。我希望这个空间显示CoordinatorLayout的背景。 - A.A.
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - mVck
这个很好用...直到SnackBar将FAB向上推,但是RecyclerView没有跟着一起移动。有人有解决方法吗?需要在CoordinatorLayout中为RecyclerView添加一些行为吗? - Tenfour04

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接