如何更改 Kotlin 中 RecyclerView 中每个 Even 值元素的颜色?

How to change color of every Even values element in RecyclerView in Kotlin?

我对 kotlin 比较陌生,请告诉我一下

//In onBindViewHolder method :
        val context = holder.itemView.context
//Here I want to change background colour so please tell me or comment
        if (position % 2 == 0) {
           holder.binding.root.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent))
        } else {
            holder.binding.root.setBackgroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
        }

提前致谢!

请使用这个:

 if (position % 2 == 1) {
           holder.binding.root.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent))
        } else {
            holder.binding.root.setBackgroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
        }

会被执行