无法在Recyclerview下方添加空视图

31

我正在尝试使用recyclerview和floating action button来开发一个应用程序。我遇到的问题是,floating action button与recyclerview中的按钮相互干扰。我查阅了Android应用程序(例如在网格视图中显示联系人的电话应用程序)的设计方式来解决这个问题,我发现留下了底部的空白以在recyclerview和floating action button之间提供空间,用户只需滚动到底部即可避免重叠。

我尝试在recyclerview后添加一个view,但它没有显示出来,而且这个view应该随着recyclerview的行移动。我缺少什么来展示recyclerview末尾的view?

我的布局文件:

<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最后绘制即可。请查看我的回答中的示例。希望对你有所帮助。 - Dpedrinha
4个回答

50

为RecyclerView添加底部填充。另外,除非您在布局管理器中覆盖了onMeasure方法,否则不要使用android:layout_height="wrap_content"属性。当前的布局管理器还不支持包裹内容。

添加属性android:clipToPadding="false"以实现此目标。 如评论中所述。


1
为RecyclerView添加填充会导致屏幕的一部分被占用,从而切断了屏幕末尾行的视图。它不会在最后一行下面显示空白空间,而是显示在最后一行上方。 - Psypher
19
刚解决了那个问题...我们还需要在RecyclerView中添加android:clipToPadding="false"。这是对我有效的最终结果。<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"/> - Psypher
@ɥʇᴉɾuɐɹ 太棒了,你让我开心了 :-) - cgr
实际上它支持wrap_content。问题在于,当新元素绘制在屏幕上后,它不会重新计算。但是对于之前的元素,它可以正常工作。所以你需要做的就是将RecyclerView放在布局文件的最后。然后你可以使用RelativeLayout设置元素的位置。请查看我的答案。 - Dpedrinha
这个解决方案会在向上滚动时使我的底部视图消失。有人知道为什么吗? - eskalera
显示剩余2条评论

3
这是一个示例(在此情况下为listView,但可以是任何东西),位于recycler view下方,占用父元素其余的高度。请注意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"
    />


1
我已经为此创建了一个实用方法,因此您可以轻松添加填充。
public void addLastChildPadding(RecyclerView.ViewHolder holder,int padding,int recyclerListSize,int position){

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

0

立即尝试这个

<?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>

根据您的需求删除图像和其他字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接