Search code examples
androidandroid-recyclerviewlinearlayoutmanager

RecycleView height wrap_content giving blank space on every Item


enter image description here

This is RecycleView code :

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvMedicine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/recycleview_add_medicine"
    android:scrollbars="vertical"/>

And This is RecycleView Item code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:weightSum="2"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#03A9F4"
                android:layout_weight="1.3">
                <TextView
                    android:id="@+id/tvMedicineName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="10dp"
                    android:textSize="16dp"
                    android:textColor="#ffffff"
                    android:layout_centerInParent="true"
                    android:textAlignment="center"
                    android:text="Maxican Pasta"/>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_weight=".7"
                android:background="@color/colorPrimary"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tvMedicineQuantity"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#ffffff"
                    android:gravity="center"
                    android:text="100 pics"
                    android:textSize="18dp"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>

        </LinearLayout>
</LinearLayout>

What's problem in this code?


Solution

  • Chang this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   <-- change is here -->
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    

    This xml file is of each row of RecylerView so when you use match_parent then each row height will we set to same height of <android.support.v7.widget.RecyclerView . When you use wrap_content at that time every row height will be adjusted as required, height may vary But in match_parent height of each row is fixed to the height of <android.support.v7.widget.RecyclerView

    Hope you understood.