带有 FloatingActionButton 的 RecyclerView

RecyclerView with FloatingActionButton

我有一个 activity,它有一个 RecyclerView(可选 CardView)和一个 FloatingActionButton

我希望 FAB 始终显示在屏幕右下角,但是当我滚动到 RecyclerView 末尾时,FAB 隐藏了最后一项的部分内容。

在父 CardView 上使用 android:layout_marginBottom="48dp"(或删除 CardViewRecyclerView 本身)修复了该问题,但它会导致 RecyclerView缩小到屏幕尺寸减去 48dp 边距。

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

我见过很多类似(但不相同)的问题,但 none 的解决方案对我有用。我知道这个问题应该有一些简单的修复方法,我不想扩展 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>

从 CardView 中删除 layout_marginBottom 并将以下内容添加到 RecyclerView:

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