TextView在RecyclerView中仅显示第一个字母

时间:2019-05-06 09:15:24

标签: android-recyclerview textview android-constraintlayout android

在我的应用程序中,我有一个RecyclerView,用于显示带有图像和标题的产品。布局使用ConstraintLayout(版本2.0.0-alpha5),TextView宽度基于图像宽度。 我希望标题为singleLine,所以我声明了maxLines = 1(我也使用singleLine属性来查看是否是问题所在),并且我希望显示第一个单词,但是(显然)它只是随机显示第一个字母。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                               xmlns:app="http://schemas.android.com/apk/res-auto"
                                               xmlns:tools="http://schemas.android.com/tools"
                                               android:layout_width="wrap_content"
                                               android:layout_height="match_parent"
                                               android:layout_margin="4dp">

<androidx.cardview.widget.CardView
        android:id="@+id/product_img_card"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/product_designation">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/product_img_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

        <ImageView
                android:id="@+id/product_img"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintDimensionRatio="V,1:1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:src="@drawable/img_product_sample"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

<TextView
        android:id="@+id/product_designation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:textSize="9sp"
        android:fontFamily="sans-serif-smallcaps"
        android:background="#FF0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/product_img_card"
        app:layout_constraintStart_toStartOf="@+id/product_img_card"
        app:layout_constraintTop_toBottomOf="@+id/product_img_card"
        tools:text="COUTEAU PLAT 16CM BOIS"
        android:maxLines="1"/>

我的RecyclerView适配器onBindViewHolder方法如下:

  override fun onBindViewHolder(holder: ProductHolder, position: Int) {
    val product = listProducts[holder.layoutPosition]
    holder.designation.text = product.designation
    Log.d(
        "MyProblem",
        "Product.designation=[${product.designation}], textview content=[${holder.designation.text}]"
    )
    val photo = PhotosUtils.getFirstroductPicture(holder.itemView.context, product.codeArticle)
    if (photo != null) {
        val uri = Uri.fromFile(photo)
        GlideApp.with(holder.img)
            .load(uri)
            .error(R.drawable.ic_broken_image_black_24dp)
            .signature { photo.length().toString() }
            .into(holder.img)
    } else {
        GlideApp.with(holder.img)
            .load(R.drawable.ic_broken_image_black_24dp)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            //.signature { System.currentTimeMillis() }
            .into(holder.img)
    }
}

图片正确显示,product.designation和textview.text值正确(例如,“ JERRICAN PLASTIQUE 5L”),但是有时候,我在TextView中只显示了第一个字母。 背景颜色显示正确的宽度。

在下面的屏幕截图中,我首先看到一个产品的全部字样,然后在RecyclerView中滚动并返回到正确显示的该产品,它只再次显示了第一个字母。

enter image description here

为什么会这样?

0 个答案:

没有答案
相关问题