我有一个活动,其中包含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>
layout_marginBottom
。paddingBottom
是其中一次尝试的结果。已更新问题。尝试了您的建议,包括和不包括layout_marginBottom
,但仅在RecyclerView
的末尾添加了额外的空白空间。 - A.A.CoordinatorLayout
的背景。 - A.A.