无法在Recyclerview下面添加空视图

时间:2015-01-04 17:49:40

标签: android android-layout android-recyclerview recycler-adapter

我正在尝试使用recyclerview和浮动操作按钮来创建应用程序。我遇到的问题是浮动操作按钮阻碍了recyclerview中的按钮。我查看了Android应用程序如手机应用程序(你在gridvew中显示的联系人)是如何设计来处理这个问题的,我看到底部留有空白空间,在recyclelerview和浮动操作按钮之间留出空间,用户只需滚动到底部以避免重叠。 我尝试在recyclelerview之后添加一个视图,但不会显示它,此视图也应该与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>

enter image description here

更新:修改布局代码以使其正常工作。

<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"/>

4 个答案:

答案 0 :(得分:45)

将底部填充添加到RecyclerView。 此外,除非您在布局管理器中覆盖android:layout_height="wrap_content",否则请勿使用onMeasure。当前的布局管理器尚不支持换行内容。

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

答案 1 :(得分:2)

这是一个示例(在这种情况下是一个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"
    />

答案 2 :(得分:1)

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

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

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

答案 3 :(得分: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>

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

相关问题