微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RecyclerView项目大小在滚动时更改

如何解决RecyclerView项目大小在滚动时更改

第一次recyclerView加载时,它将显示所有正常值。但滚动后,布局项目的大小已更改。

enter image description here

这是我的item_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/item_border"
    android:layout_margin="@dimen/item_margin_2">
    <ImageView
        android:id="@+id/imageProduct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
         />

    <TextView
        android:id="@+id/textProductTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="@dimen/title_textSize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageProduct"
        tools:text="TubeLight" />

    <TextView
        android:id="@+id/textProductDetailSort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:textColor="@android:color/black"
        android:textSize="@dimen/detail_textSize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textProductTitle"
        tools:text="warranty: 100 days" />


    <LinearLayout
        android:id="@+id/linerCartItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textProductDetailSort">

        <ImageButton
            android:id="@+id/buttonRemove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/item_border_small"
            android:src="@drawable/ic_outline_remove_24"/>

        <TextView
            android:id="@+id/textNumberItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryLight"
            android:textSize="@dimen/detail_small_textSize"
            android:padding="@dimen/item_padding"
            android:textColor="@color/black"
            android:layout_margin="@dimen/item_margin_3"
            android:text="0"/>

        <ImageButton
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/item_border_small"
            android:src="@drawable/ic_baseline_add_24"/>

    </LinearLayout>


    <TextView
        android:id="@+id/textViewProductPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/linerCartItem"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="@dimen/item_margin"
        android:text="@string/title_price"/>


</androidx.constraintlayout.widget.ConstraintLayout>

这是adapter class

class Productlistadapter(context: Context,private val cellClickListener: HomeFragment) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    interface CellClickListener {
        fun onCellClickListener(productModel: ProductModel)
        fun onNextClicked()
        fun onPrevClicked()
        fun onAddClicked(productId: String): Int
        fun onRemoveClicked(productId: String): Int
    }

    val FOOTER_TYPE : Int = 1
    val HEADER_TYPE : Int = 2
    var context : Context = context

    private var listofProducts = listof<ProductModel>()


    override fun getItemViewType(position: Int): Int {

         if (position ==  listofProducts.size) {

             return FOOTER_TYPE
         }
//         else if(position == 0){
//            return HEADER_TYPE
//         }
        return 0

    }
    override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): RecyclerView.ViewHolder {

        if (viewType == FOOTER_TYPE){
            return ProductListViewHolderFooter(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.item_product_footer,parent,false
                )
            )
        }else if(viewType == HEADER_TYPE){
            return ProductListViewHolderHeader(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.item_product_header,false
                )
            )
        }
        else{
            return ProductListViewHolder(
                context,LayoutInflater.from(parent.context).inflate(
                    R.layout.item_product,false
                )
            )
        }

    }

    override fun getItemCount(): Int = listofProducts.size + 1

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder,position: Int) {
        try {
            if (viewHolder is ProductListViewHolder) {
                val vh: ProductListViewHolder = viewHolder as ProductListViewHolder
                vh.bindView(listofProducts[position],cellClickListener)

            } else if (viewHolder is ProductListViewHolderFooter) {
                val vh: ProductListViewHolderFooter = viewHolder as ProductListViewHolderFooter
                vh.bindViewFooter(listofProducts[position],cellClickListener)
            }
        } catch (e: Exception) {
            e.printstacktrace()
        }


    }

    fun setProductList(listofProducts: List<ProductModel>) {

        this.listofProducts = listofProducts
        notifyDataSetChanged()
    }
}

这是holder

class ProductListViewHolder(context: Context,itemView: View) : RecyclerView.ViewHolder(itemView){

   
    var sharedPreferences: SharedPreferences = context.getSharedPreferences(utils.PREFERENCE_FILE_NAME,Context.MODE_PRIVATE)
  

    fun bindView(productModel: ProductModel,cellClickListener: HomeFragment) {
        itemView.textProductTitle.text = productModel.productTitle
        itemView.textProductDetailSort.text = productModel.productDetailSort


        itemView.textViewProductPrice.text = context.getString(R.string.title_price) + ": " + productModel.price

       
        itemView.textNumberItem.text = cart

        Glide.with(itemView.context).load(productModel.productimageUrl!!).into(itemView.imageProduct)

         itemView.imageProduct.setonClickListener{
             cellClickListener.onCellClickListener(productModel)
         }

         itemView.buttonRemove.setonClickListener {
             val cart = cellClickListener.onRemoveClicked(productModel.productId)
             itemView.textNumberItem.text = cart.toString()
         }

         itemView.buttonAdd.setonClickListener {
             val cart = cellClickListener.onAddClicked(productModel.productId)
             itemView.textNumberItem.text = cart.toString()
         }

    }


}

解决方法

由于无法控制ImageView的高度而导致的问题,导致视图回收过程中尺寸的变化。要解决此问题,您需要在imageProduct属性中进行一些更改。

首先,在将imageProduct的开始和结束绑定到父级的开始和结束时,应将视图宽度设置为0dp,以确保它充满整个空间。

第二,将adjustViewBounds属性设置为true。它会导致调整ImageView边界以保留其可绘制对象的长宽比。

第三,将layout_constrainedHeight属性设置为true。这样可以确保视图的高度受到限制,并且还可以使视图的高度保持为wrap_content

<ImageView
    android:id="@+id/imageProduct"
    android:layout_width="0dp"                      // 1
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher_foreground"
    android:adjustViewBounds="true"                 // 2
    app:layout_constrainedHeight="true"             // 3
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

此外,如果您希望始终以特定的宽高比显示图像,则可以通过将高度更改为0dp并使用layout_constraintDimensionRatio属性定义该比例来实现,类似于以下内容:

<ImageView
    android:id="@+id/imageProduct"
    android:layout_width="0dp"                     
    android:layout_height="0dp"                     // setting the height constrainted
    android:contentDescription="@string/app_name"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher_foreground"
    android:adjustViewBounds="true"      
    app:layout_constraintDimensionRatio="1:1"       // to show as a square  
    app:layout_constrainedHeight="true"             
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
,

尝试致电滑行清除Glide.with(itemView.context).clear(itemView.imageProduct) 在顶部 Glide.with(itemView.context).load(productModel.productImageUrl!!).into(itemView.imageProduct)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。