Search code examples
androidandroid-recyclerviewgridlayoutmanager

GridLayoutManager gives same height to item in the same lines


I have a recycler view with GridLayoutMananger and for some reason all the items in the same lines have the same height It looks like this : enter image description here

As you can see, all the elements have the same height. There is an empty space below the red line I drew on the picture ; I would it to be removed. The right item has a higher height than the ohters so the two others items takes the same height.

I would like every item to take the height that it needs and no more.

Here is code in java :

GridLayoutManager layoutManager = new GridLayoutManager(this,3 );
    recycle_confirm_command = (RecyclerView) findViewById(R.id.recycle_all_command);
    recycle_confirm_command.setLayoutManager(layoutManager);
    adapter = new ListCommandAdapter(this, List_to_confirm, btn_total);
    recycle_confirm_command.setAdapter(adapter);

And here the XML adapter for the item :

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    card_view:cardBackgroundColor="@color/UnderPrim"
    card_view:cardCornerRadius="10dp"
    android:background="@drawable/my_button_xml">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/lbl_command"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="eeee\nrfkfkkf\nfkfkfkf\ndkdkdkd\j\nkdkdkd"
            android:layout_toLeftOf="@id/lbl_time_of_command"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/lbl_time_of_command"
            android:layout_width="wrap_content"
            android:text="time"
            android:layout_alignLeft="@id/lbl_numer_of_command"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/lbl_numer_of_command"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="bottom"
            android:layout_alignBottom="@id/lbl_command"
            android:text="numéro"
            android:layout_below="@id/lbl_time_of_command"
            android:layout_height="wrap_content" />



    </RelativeLayout>


</android.support.v7.widget.CardView>

The CardView has "wrap content" as height because the weight is defined by the height of the left textview. Thank you for you help


Solution

  • You need to use a StaggeredGridLayoutManager to have different heights for each item, ie to have height as wrap content. Replace your GridLayoutManager with the below StaggeredGridLayoutManager

    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
    

    enter image description here