图像在滚动时在recylerview中被替换,android?

问题描述 投票:0回答:4

我正在recylerview中显示国家/地区及其标志第一个元素没有图片,并使用默认图片,该图片在启动页面时可见但是,当我滚动并返回到图像时,图像会从列表中更改为某种随机现象,这是不应该发生的

这是我的适配器

class CountryAdapter(private val list: MutableList<Data?>?) :
RecyclerView.Adapter<CountryAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val binding = ElementCountryBinding.inflate(inflater)
    return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val country: Data? = list?.get(position)
    if (country != null) {
        holder.bind(country)
    }
    holder.itemView.setOnClickListener {

    }
}

override fun getItemCount(): Int = list!!.size

inner class ViewHolder(val binding: ElementCountryBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(country: Data) {
        binding.data = country
        if (country.filePath != null)
            Glide.with(binding.root.context)
            .load(country.filePath!!.trim()).into(binding.ivFlag)
    }
}
}

这是xml布局

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

<data>


    <variable
        name="data"
        type="com.mountmeru.model.Data" />
</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:id="@+id/main_cardview"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginBottom="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/iv_flag"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:layout_marginStart="10dp"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_share"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvCountryName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="@{data.countryName}"
                app:layout_constraintBottom_toBottomOf="@+id/iv_flag"
                app:layout_constraintLeft_toRightOf="@+id/iv_flag"
                app:layout_constraintTop_toTopOf="@+id/iv_flag" />
        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.cardview.widget.CardView>
    < /RelativeLayout>
 </layout>

截图

Without scrolling

After scrolling.

android kotlin android-recyclerview imageview
4个回答
1
投票

因此,您在XML布局中指定的默认图像是ic_share,这意味着在调用onBindViewHolder时,该图像将被替换为:

.load(country.filePath!!.trim()).into(binding.ivFlag)

但是,您从未指定位置0处的图标必须为ic_share,因此由于RecyclerView的本质,当您上下滚动并再次创建第一个itemHolder时,它将使用从下往下的回收视图,并且您没有在位置0处将ic_share设置为iv_flag,它只是使用回收的视图图像。

如果您只是在bind方法中添加像@ADM这样的代码,就像这样:

if(adapterPosition==0){
            binding.ivFlag.setImageResource(R.drawable.ic_share)
        }

使用ic_share,我认为应该可以使用它


1
投票

由于RV / LV中视图的回收机制,这是正常的。为了避免这种情况,请将其设置为null

 if (country.filePath != null)
     Glide.with(binding.root.context)
       .load(country.filePath!!.trim()).into(binding.ivFlag)
else
     binding.ivFlag.setImageDrawable(null)

假设ivFlagImageView,或者如果有,则为默认/占位符


1
投票

之所以发生这种情况,是因为您从未在绑定视图期间设置ic_share

inner class ViewHolder(val binding: ElementCountryBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(country: Data) {
        binding.data = country
        if(adapterPosition==0){
            binding.ivFlag.setImageResource(R.drawable.binding.ivFlag)
        }else {
            if (country.filePath != null)
                Glide.with(binding.root.context)
                    .load(country.filePath!!.trim()).into(binding.ivFlag)
        }
    }
}

0
投票

具有getItemViewType函数也可以解决问题

override fun getItemViewType(position: Int): Int {
    return position
}
© www.soinside.com 2019 - 2024. All rights reserved.