Search code examples
androidandroid-layoutandroid-recyclerviewandroid-linearlayout

Unable to WRAP_CONTENT RecyclerView inside LinearLayout


Basically I have two similar layouts with different approach to create. The problem is that RecyclerView WRAP_CONTENT behaves differently on those 2 layouts. At the first layout it works fine, while at the others, WRAP_CONTENT doesn't work.

The code below doesn't work.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    ... other layout codes
    <android.support.v7.widget.RecyclerView
        android:id="@+id/xxx"
        style="@style/MarginLeftDetail"
        android:layout_width="match_parent"
        android:layout_height="0dp"
    />
     ... other layout codes
</LinearLayout>

While this is the one that is working.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    ... other layout codes
    <android.support.v7.widget.RecyclerView
        android:id="@+id/xxx"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/yyy"
        style="@style/MarginLeftDetail"
        android:layout_marginTop="7.5dp"/>
    ... other layout codes
</RelativeLayout>

Does LinearLayout interpret WRAP_CONTENT differently than RelativeLayout?

UPDATE : If I wrap the first RecyclerView with RelativeLayout, it works fine. So I guess LinearLayout really interpret WRAP_CONTENT differently? Or is it a bug?

Thanks.


Solution

  • You should add android:layout_weight=1 to RecyclerView if you are giving android:height="0dp" Change your code as follow

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        ... other layout codes
        <android.support.v7.widget.RecyclerView
            android:id="@+id/xxx"
            style="@style/MarginLeftDetail"
            android:layout_width="match_parent"
            android:layout_weight= "1"
            android:layout_height="0dp"
        />
         ... other layout codes
    </LinearLayout>