【问题标题】:Recyclerview items not showing with the same heightRecyclerview 项目没有以相同的高度显示
【发布时间】:2019-06-21 08:16:19
【问题描述】:

This is my app where is the problem exists

I want my items to fit like this app

我正在使用RecyclerViewGridLayoutManager。我在图像中看到的问题是,当其中一个项目的文本较长时,网格项目的高度不同。我正在从 firebase 加载数据,并在列表中有数据时通知Adapter。我尝试了一切,但找不到像附加的第二张图片一样自动调整项目的解决方案。请帮忙?

这是我的 recyclerview 项目布局

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    >



    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:cardCornerRadius="10dp"
        android:layout_margin="5dp"
        app:cardBackgroundColor="#fff"
        app:cardElevation="5dp"
        >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/cartoonImg"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:scaleType="fitXY"
                app:imgUrl="@{cartoon.thumb}"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tool:text="Cartoon Name"
                android:gravity="center"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="2dp"
                android:layout_marginLeft="2dp"
                android:textColor="@color/colorPrimary"
                android:text="@{cartoon.title}" />

        </LinearLayout>

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

</FrameLayout>

【问题讨论】:

  • 嗨,欢迎来到 StackOverFlow。请提供recyclerviewitem.xml的布局
  • 你需要更改height of the item wrap_content to 100sp or what ever you liked
  • 我添加了我的 recyclerview 项目布局 ankuranurag2
  • Vishal Yadav 我想要一种自动调整方式而不是固定尺寸。

标签: java android android-recyclerview gridlayoutmanager


【解决方案1】:

这将完全满足您的需求

尝试用这个替换你的TextView

           <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tool:text="Cartoon Name"
                android:gravity="center"
                android:minLines="2"
                android:maxLines="2"
                android:ellipsize="end"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="2dp"
                android:layout_marginLeft="2dp"
                android:textColor="@color/colorPrimary"
                android:text="@{cartoon.title}" />

【讨论】:

  • 即使没有需要这两行的标题,它也会始终显示该空间,我猜他想要动态,如果只有一个项目是两行,则全部为 2 行,否则为 1 行和正常高度
【解决方案2】:

你正在使用这条线

android:layout_height="wrap_content"

在您的CardView 上,并且由于后代有match_parent,因此您的TextView 的大小会发生变化。

要解决此问题,您可以在 TextView 中使用以下属性 android:maxLines="1"。这将阻止 TextView 跳转到下一行,但会裁剪文本。

或者,您可以使用静态高度来代替match_parentwrap_content,即使用一个值。我强烈推荐使用48dp,因为这是Material Design Guidelines 推荐的。 来源:

【讨论】:

    【解决方案3】:

    您可以添加到您的 TextView:

    android:ellipsize="start" android:maxLines="1

    从 API 26 开始,您可以使用 Autosizing TextView - 在此处查看文档:https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

    【讨论】:

      【解决方案4】:

      你可以给你的文本视图一个静态高度,这样它对所有人都是一样的。但是您可能会在其中丢失文本。

      【讨论】:

      • 我知道。这就是为什么我需要一种自动调整方式而不是固定高度的原因。
      猜你喜欢
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 2021-09-02
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多