【问题标题】:RecyclerView with FloatingActionButton带有 FloatingActionButton 的 RecyclerView
【发布时间】:2017-02-13 02:22:51
【问题描述】:

我有一个包含RecyclerView(可选在CardView)和FloatingActionButton 的活动

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

在父级CardView 上使用android:layout_marginBottom="48dp"(或删除CardView 后的RecyclerView 本身)修复了该问题,但它会导致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>

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 其实我用的是layout_marginBottompaddingBottom 来自其中一项试验。更新了问题。在有和没有layout_marginBottom 的情况下尝试了你的建议,但它只在RecyclerView 的末尾添加了额外的空格
    • 对不起@A.A.它是 clipToPadding="false"(不是 "true")。我编辑了我的答案。
    • 这与我想要的非常接近,但列表末尾仍有空白。我希望这个空间改为显示CoordinatorLayout 的背景
    • @A.A.我想你可以使用 RecyclerView 的背景(可能设置一些透明度)和它的项目的背景来拥有你想要的。
    • 这很好用……直到 SnackBar 将 FAB 向上推,而不是 RecyclerView。有人对此有解决方案吗? CoordinatorLayout中的RecyclerView是否需要添加一些行为?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2016-01-17
    • 2015-08-15
    • 2016-05-14
    • 1970-01-01
    • 2019-04-05
    • 2017-10-10
    相关资源
    最近更新 更多