如何在RecyclerView中设置CardViews之间的等间距?

3

我在我的RecyclerView中有一些CardViews,当我想通过添加边距来设置它们之间的间隔时,我得到了这个结果:

enter image description here

如您所见,卡片之间的距离是双倍的。如果我仅为顶部、左侧和右侧设置边距,则问题得到解决,但最后一张卡片将接触屏幕底部,而我不希望出现这种情况。就像这样:

enter image description here

作为您可以看到的,所有空格都是相等且良好的,但底部没有空格。我在我的项目中有一个MainActivity.java、activity_main.xml、recyclerview_row.xml和MyAdapter.java。
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

这是我的recyclerview_row.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="false"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/image_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

我的问题是如何设置这些卡片之间的等距。谢谢。

在你的activity_main.xml的父布局中设置底部填充。 - AbhayBohra
我之前尝试过这个,但它会在底部留下一个空白,滚动时不太好。 - Arantik
@Arantik,您需要在右下角添加一些空间,或者像列表中的其他项目一样,在最后一个项目处添加一些空间。 - Nithinlal
一个解决方案是在适配器的getView方法中,对于最后一项添加一个padding。我认为没有直接的解决方案。 - Nithinlal
4个回答

4
将您的activity_main.xml更改为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
    android:paddingBottom="10dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>

这个魔法是通过这一行实现的:android:clipToPadding="false"


通过为CardView设置左、右和顶部边距,然后使用这个解决方案,我的问题得到了解决。非常感谢! - Arantik
@Arantik 不用谢 :) - Mehul Kanzariya

1

在主活动中尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

0
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:paddingBottom="@dimen/dim_10"
        android:background="#dddddd">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view">
        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>

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