用于网格视图的 Android 自定义 BaseAdapter 在回滚时返回其他项目

问题描述

我为我的 GridView 实现了一个自定义 BaseAdapter,当我第一次向下滚动时,它工作正常。当我向上滚动时,所有项目似乎都被其他项目覆盖了,这些项目以前不存在。我如何防止这种情况发生,我只想再次显示旧项目:

BaseAdapter 类:

class GridBaseAdapter(context: Context): BaseAdapter () {
    
    private val context = context
    private val activity = context as CameraActivity
    private var imageArray = activity.imageArray

    override fun getView(position: Int,convertView: View?,parent: ViewGroup?): View? {
        val mInflator: LayoutInflater = LayoutInflater.from(context)
        var view: View?
        var vh: ListRowHolder
        if (convertView == null) {
            Log.d("ADAPTER",position.toString())
            view = mInflator.inflate(R.layout.grid_view_item,parent,false)
            vh = ListRowHolder(view)
            view.tag = vh

            view.setonClickListener {
                val firstimageFile = vh.firstimageFile
                val secondImageFile = vh.secondImageFile
                val fragment: CameraActivity = context as CameraActivity
                fragment.openPreviewFragment(firstimageFile,secondImageFile)
            }
        } else {
            view = convertView
            vh = view?.tag as ListRowHolder

        }
        Glide.with(context)
            .load(imageArray[position][0])
            .placeholder(R.drawable.ic_baseline_downloading_24)
            .centerCrop()
            .into(vh.firstimage)
        vh.firstimage.visibility = View.VISIBLE
        vh.firstimageFile = imageArray[position][0]
        if (imageArray[position].size > 1){
            Glide.with(context)
                .load(imageArray[position][1])
                .placeholder(R.drawable.ic_baseline_downloading_24)
                .centerCrop()
                .into(vh.secondImage)
            vh.secondImage.visibility = View.VISIBLE
            vh.secondImageFile = imageArray[position][1]
            vh.secondImage.rotation = 5F
            vh.firstimage.rotation = -5F
        }
        return view
    }

    override fun getItem(position: Int): Any {
        return imageArray[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return imageArray.size
    }

}

private class ListRowHolder(row: View?) {
    public var firstimageFile: File? = null
    public var secondImageFile: File? = null
    public val firstimage: ImageView = row?.findViewById(R.id.grid_view_item_first_image) as ImageView
    public val secondImage: ImageView = row?.findViewById(R.id.grid_view_item_second_image) as ImageView
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...