elunap elunap - 3 months ago 46
Android Question

RecyclerView, CardView, "dead" space between cards

I implemented A RecyclerView in which I display some CardViews from a list, but the problem Is that when displayed there is so much space between cards, the space is the same space the CardView has as height and only at the bottom of each card.

This is my code:

CardView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="245dp"
android:layout_gravity="center_horizontal"
cardview:cardElevation="4dp"
cardview:cardCornerRadius="5dp"
android:layout_marginBottom="0dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="240dp"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="@+id/ivProduct"
android:scaleType="centerCrop" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:text="Photo Title"
android:id="@+id/txtTipo"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:text="Photo Title"
android:id="@+id/txtdescripcion"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>


RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>


1

[![2][2]][2]

enter image description here

Answer

Your LinearLayout inside the RecyclerView is with a fill_parent height,so the height assume the screen height, just change for wrap_content.

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"         <!--change in this line-->
    android:gravity="center_horizontal"
    android:padding="5dp">
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="245dp"
    android:layout_gravity="center_horizontal"
    cardview:cardElevation="4dp"
    cardview:cardCornerRadius="5dp"
    android:layout_marginBottom="0dp">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:orientation="vertical"
        android:padding="8dp">
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:id="@+id/ivProduct"
            android:scaleType="centerCrop" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#333333"
            android:text="Photo Title"
            android:id="@+id/txtTipo"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#333333"
            android:text="Photo Title"
            android:id="@+id/txtdescripcion"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp" />
    </LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>