【发布时间】: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