【问题标题】:GridLayoutManager gives same height to item in the same linesGridLayoutManager 为同一行中的项目提供相同的高度
【发布时间】:2019-07-07 15:03:30
【问题描述】:

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

如您所见,所有元素都具有相同的高度。我在图片上画的红线下方有一个空白区域;我希望它被删除。 正确的项目比其他项目的高度更高,因此其他两个项目的高度相同。

我希望每个项目都达到它需要的高度,而不是更多。

这里是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 将“包装内容”作为高度,因为权重由左侧 textview 的高度定义。 谢谢你的帮助

【问题讨论】:

    标签: android android-recyclerview gridlayoutmanager


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 2019-04-13
      • 2021-07-27
      • 1970-01-01
      • 2022-01-21
      • 2019-03-25
      • 2022-07-26
      • 2016-05-15
      • 2016-05-11
      相关资源
      最近更新 更多