Buttons added underneath Reciclerview do not appear

Asked

Viewed 137 times

2

I’m using a Linearlayout with a Reciclerview and 3 buttons underneath it.

I’m doing this, but Reciclerview takes up the whole screen and the buttons don’t appear.

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/salmao"
    tools:context="br.com.robson.boascompras.ListasComprasActivity">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/editar"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/excluir"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/nova_lista"/>
    </LinearLayout>
</LinearLayout>

1 answer

2


Can’t use android:layout_height="wrap_content", in a Recyclerview or Listview, when the number of items exceeds the screen size.

wrap_content means "make me big enough to present all my content".
When the space required is larger than the screen, Recyclerview takes it all in, "pushing" everything under it out of the screen.

Change android:layout_height="wrap_content" for android:layout_height="0p" and weigh it down with android:layout_weight:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/salmao"
    tools:context="br.com.robson.boascompras.ListasComprasActivity">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/editar"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/excluir"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/nova_lista"/>
    </LinearLayout>
</LinearLayout>
  • Thank you!!! solved my problem! But I was left with a question, I put "android:layout_height="wrap_content" why in my logic I do not know how many elements will be placed in the list, then it would be adjusted according to the content( the amount of items), why this logic is not valid?

  • The reason is that same be "adjusted according to the content( the amount of items)", as the space required to present the content is greater.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.