我正在尝试处理我的recyclerview问题
当我向下滚动(onBindView函数)时,效果很好
但是,当滚动回到recyclerview的第一项时,一切都变得错误,如下图所示。

   package com.leaderspro.mrlawyer.adapters

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.leaderspro.mrlawyer.R
import com.leaderspro.mrlawyer.models.TODOModel
import kotlinx.android.synthetic.main.todo_list.view.*


class TODOAdapter(private val mArray: ArrayList<TODOModel>) :
    RecyclerView.Adapter<TODOAdapter.ViewHolder>() {

    var mView: View? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        mView = LayoutInflater.from(parent.context).inflate(R.layout.todo_list, parent, false)

        return ViewHolder(mView!!)
    }

    override fun getItemCount(): Int {
        return mArray.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {


        val mTODO = mArray[position]



            if (mTODO.isDone == 0) {//not complete
                holder.ivIsDone.setImageResource(R.drawable.ic_checkbox_unchecked)
            } else if (mTODO.isDone == 1) {
                holder.ivIsDone.setImageResource(R.drawable.ic_checkbox_checked)
                holder.tvTodoTask.paintFlags =
                    holder.tvTodoTask.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG //put line on done Tasks
            }

        holder.tvTodoTask.text = mTODO.task
        holder.tvTODODate.text = mTODO.date

    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val tvTodoTask = itemView.tvTodoTask!!
        val tvTODODate = itemView.tvTODODate!!
        val ivIsDone = itemView.ivIsDone!!

        val todoListMainLinear = itemView.todoListMainLinear!!

    }
}


工作良好
https://i.stack.imgur.com/6mBUJ.png

工作良好
https://i.stack.imgur.com/qcpLK.png

向后滚动时错误地调用了onBindView
https://i.stack.imgur.com/2sRsu.png

最佳答案

使用holder.adapterposition代替位置

val mTODO = mArray[holder.adapterposition]


有关更多信息,请检查:https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html?hl=en#onBindViewHolder(VH,%20int)

编辑:
正如@Pawel指出的那样,当mTodo.isDone == 0时,应清除标志。

那应该完成工作

09-26 08:52