RecyclerView with FloatingActionButton

A.A*_*.A. 10 android android-layout android-cardview floating-action-button android-recyclerview

我有一个活动,有RecyclerView(可选在a CardView)和aFloatingActionButton

我希望FAB始终位于右下方的屏幕上,但是当我滚动到结尾时RecyclerView,FAB隐藏了最后一项的部分内容.

使用android:layout_marginBottom="48dp"CardView(或RecyclerView拆卸后本身CardView)修复的问题,但它会导致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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

mVc*_*Vck 17

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

android:paddingBottom="48dp"
android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)