android - android滚动时图像在recylerview中被替换了吗?

标签 android kotlin android-recyclerview imageview

我在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.

最佳答案

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

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

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

如果只是在bind方法中添加建议的类似@ADM的代码,如下所示:
if(adapterPosition==0){
            binding.ivFlag.setImageResource(R.drawable.ic_share)
        }

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

关于android - android滚动时图像在recylerview中被替换了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62257361/

相关文章:

java - 如何显示具有不同属性的多个数据列表

java - 简单的旋转项目不可点击

android - Ant 与 Eclipse 构建 Android : Strengths of Each?

android - Fragment 中的按钮 setOnClickListener onCreateView() 或 onActivityCreated()

hibernate - Kotlin 与 JPA/Hibernate : no lazy-loading without `open` ?

java - 防止 Android One 设备中的屏幕捕获

java - 保存到内部存储文件-Android

android - 无法在对象上找到参数 [项目 ':model'] 的方法 api()

android - anko logging方法无法解决

java - 防止在fragment中的RecyclerView中滚动时FAB下降