Bruno Henrique Paiva
Bruno Henrique Paiva

Reputation: 83

RecyclerView fill empty space inside LinearLayout

Background

I have a header and footer (LinearLayout) with a RecyclerView between them.

The problem

I need that the footer stay fixed at the bottom of the screen and the RecyclerView fills the empty space

enter image description here

What I've tried

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/constraintLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:orientation="vertical"
              tools:context="br.com.sigane.coletordifal.activity.EnderecamentoActivity">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Header" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
                  android:id="@+id/produtos_list"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scrollbars="vertical" />
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Footer" />
    </LinearLayout>
</LinearLayout>

Also tried to use ConstraintLayout, but without success

Upvotes: 2

Views: 1023

Answers (3)

ariochdivij666
ariochdivij666

Reputation: 406

I recommend using the constraint layout, it is a FrameLayout on steroids. If you are using android studio 3.2, right-clicking on the root element in the design view shows a menu with the option "convert to constraintlayout"

The reason you do not want to nest layouts like this, Each nested element results in additional measure passes, nested layouts are messy and harder to inflate than flat layouts.

Highly recommend spending an hour or so reading about the constraint layout and how to use it. You will not regret it, helped my improve productivity by 2x.

Hope this is helpful.

Upvotes: 0

ant2009
ant2009

Reputation: 22556

I don't think you need to have separate linear layouts. I think just one is enough. Just set your weights like this for your recyclerview and footer.

 <android.support.v7.widget.RecyclerView
        android:id="@+id/produtos_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:scrollbars="vertical" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
        android:text="Footer" />

Upvotes: 0

Sourabh
Sourabh

Reputation: 8492

Try this

Add android:layout_height="0dp" and android:layout_weight="1" to the RecyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/produtos_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Footer" />

    </LinearLayout>

</LinearLayout>

Upvotes: 3

Related Questions