2

I'm trying to make my Recycler view to not take all the screen space when there are just a few items on it using wrap_content, but that doesn't seem to work. What's the problem?

enter image description here Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal"
        android:weightSum="1">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/smoke_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:paddingTop="4dp"
            android:paddingRight="4dp"
            android:paddingLeft="4dp"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="25dp"
            android:background="#FFF"
            android:elevation="2dp" />
    </LinearLayout>
</RelativeLayout>
Milen Pivchev
  • 1,417
  • 2
  • 16
  • 29

4 Answers4

4

The most important thing is that RecyclerView does not support wrap_content by default. You have to struggle a bit and provide it with a LayoutManager that can force your RecyclerView to wrap its contents.

Take a look at this and this.

Community
  • 1
  • 1
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
3

use android:layout_weight="1" in RecyclerView not 0.8 and Linear layout android:layout_width="wrap_content" not fill_parent

Anil
  • 1,087
  • 1
  • 11
  • 24
1

You provided LinearLayout width to fill parent and weightSum to 1. Then set the layout_weight to 0.8. So, your total layout width size is 1 and you set layout_weight of recycleview to 0.8 so it takes the 80% of width.

SANAT
  • 8,489
  • 55
  • 66
1

try this...

change height from wrap_content to match_parent

<android.support.v7.widget.RecyclerView
        android:id="@+id/smoke_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:paddingTop="4dp"
        android:paddingRight="4dp"
        android:paddingLeft="4dp"
        android:background="#FFF"
        android:elevation="2dp" />
Iamat8
  • 3,888
  • 9
  • 25
  • 35