无法在 Recyclerview 下方添加空视图

Not able to add empty view below Recyclerview

我正在尝试使用应用程序的 recyclerview 和浮动操作按钮。我遇到的问题是浮动操作按钮阻碍了recyclerview中的按钮。我查看了 android 应用程序,例如 phone 应用程序(您在 gridvew 中显示联系人的应用程序)是如何设计来处理这个问题的,我看到 space 中剩下空的底部在 recyclerview 和浮动操作按钮之间给出 space,用户只需滚动到底部即可避免重叠。 我试图在 recyclerview 之后添加一个视图,但没有显示它,而且这个视图应该与 recyclerview 行一起移动。我缺少什么来显示 recyclerview 的末尾的视图。 我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#6fbababa">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"/>

    <ImageButton
        android:id="@+id/button_floating_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_fab"
        android:background="@null"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:elevation="2dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_below="@id/my_recycler_view"
        android:background="@color/white"/>

</RelativeLayout>

更新: 修改了布局代码使其工作。

<android.support.v7.widget.RecyclerView 
android:id="@+id/my_recycler_view" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clipToPadding="false" 
android:paddingBottom="30dp" 
android:scrollbars="vertical"/>

向 RecyclerView 添加底部填充。 另外,除非您在布局管理器中覆盖了 onMeasure,否则不要使用 android:layout_height="wrap_content"。当前的布局管理器尚不支持环绕内容。

添加属性android:clipToPadding="false"以达到目的。 如评论中所述。

这是一个位于回收器视图下方的示例(在本例中为 listView,但可以是任何东西),它占据了父级的其余高度。注意 layout_alignParentBottom 和 layout_above 属性。

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    **android:layout_alignParentBottom="true"**
    />

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_above="@+id/list_view"
    />

我为此创建了一个实用方法,因此您可以轻松添加填充

public void addLastChildPadding(RecyclerView.ViewHolder holder,int padding,int recyclerListSize,int position){

if(recyclerListSize-1==position){
holder.itemView.setPadding(0,0,0,padding);
}
}

马上试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false">

        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal">

            <android.support.v7.widget.AppCompatButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:layout_weight="1"
                android:background="@drawable/button_primary_color_border"
                android:text="70%"
                android:textColor="@android:color/darker_gray"
                android:textSize="20sp" />

            <android.support.v7.widget.AppCompatButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:layout_weight="1"
                android:background="@drawable/button_primary_color_border"
                android:text="60%"
                android:textColor="@android:color/darker_gray"
                android:textSize="20sp" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>
</LinearLayout>

</LinearLayout>

根据您的用途删除图片和其他字符串。