【问题标题】:Android Recyclerview item Textview won't show up in some itemAndroid Recyclerview 项目 Textview 不会出现在某些项目中
【发布时间】:2020-06-05 03:17:48
【问题描述】:

所以在我创建的 Recyclerview 中,我使用 CardView 设置项目布局。这是项目布局:

product_list_item_layout.xml

<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/CardView"
    android:id="@+id/sampleProductCard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="12dp"
    app:cardCornerRadius="12dp"
    app:cardElevation="6dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher_background"
            />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="Apple Pie"
                android:textColor="#6f6f6f"
                android:textSize="14sp"
                android:id="@+id/productName"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:text="Rp. "
                    android:textColor="#6f6f6f"
                    android:textSize="12sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginLeft="2dp"
                    android:text="Price"
                    android:textColor="#6f6f6f"
                    android:textSize="12sp"
                    android:id="@+id/productPrice"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:text="Stock :"
                    android:textColor="#000"
                    android:textSize="10sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_marginLeft="6dp"
                    android:text="4"
                    android:textColor="#000"
                    android:textSize="10sp"
                    android:id="@+id/productStockQuantity"
                    />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</com.google.android.material.card.MaterialCardView>

在 Android Studio 中,设计预览显示为 this

这是recyclerview布局:

fragment_product_list.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    tools:context=".mainFragments.ProductListFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        ...

        ...


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvProductList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            />

        ...

    </LinearLayout>

</layout>

在片段中,我将其设置为 GridLayout。但它显示为this

关于细节,product_list_item.xmlandroid:id="@+id/productName" 中的 textview 的长度有很多变化,因为我从 API 端点获取数据。

我认为片段或 recyclerview 适配器没有任何问题。

我在这里错过了什么吗?如果有任何我遗漏的细节,请在评论中提及。

【问题讨论】:

  • 能不能把ImageView的高度改成wrap_content
  • 我明白了。多谢,伙计。现在Textview问题解决了。
  • 不客气,兄弟。我把它作为答案,将来可能会帮助其他人

标签: android android-recyclerview


【解决方案1】:

请尝试将ImageView的高度改为wrap_content

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher_background"
        />

【讨论】:

    【解决方案2】:

    由于我的声誉,我无法发表评论。

    这是我的一些想法

    • 布局是否始终重复该模式?或者数据可能不存在并且由于布局设置为包装内容,它隐藏了文本视图?也许尝试放置一些日志,看看发生了什么。
    • 对于回收站视图项目,您的布局结构似乎有点复杂。也许用约束布局把它弄平?如果您尝试让 imageview 用几条在线文本填充整个布局,那就更容易了。

    【讨论】:

      【解决方案3】:

      检查您是否在其他单元格中设置了数据。 有时 recyclerView 对空单元格不正确。 检查您是否填写包括名称、价格、数量等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        • 2019-12-01
        • 2020-12-15
        相关资源
        最近更新 更多