GridLayoutManager 为同一行中的项目提供相同的高度

GridLayoutManager gives same height to item in the same lines

我有一个带有 GridLayoutMananger 的回收器视图,出于某种原因,同一行中的所有项目都具有相同的高度 它看起来像这样:

如您所见,所有元素的高度都相同。我在图上画的红线下面有一个空的space;我希望它被删除。 右边的项目比其他项目的高度更高,所以其他两个项目的高度相同。

我希望每件物品都能达到它需要的高度,不要超过。

这是 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);

这里是项目的 XML 适配器:

 <?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>

CardView 的高度为 "wrap content",因为权重由左侧文本视图的高度定义。 谢谢你的帮助

您需要使用 StaggeredGridLayoutManager 为每个项目设置不同的高度,即将高度作为包装内容。用下面的 StaggeredGridLayoutManager

替换你的 GridLayoutManager
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);