【问题标题】:How to change color of every Even values element in RecyclerView in Kotlin?如何更改 Kotlin 中 RecyclerView 中每个偶数值元素的颜色?
【发布时间】:2020-06-04 17:21:48
【问题描述】:

在 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))
        }

提前致谢!

【问题讨论】:

  • 所有发布的都是程序描述。但是,我们需要您ask a question。我们无法确定您想从我们这里得到什么。请edit您的帖子包含一个我们可以回答的有效问题。提醒:请确保您知道what is on-topic here,请我们为您编写程序,建议是题外话。

标签: java android kotlin kotlin-coroutines kotlin-android-extensions


【解决方案1】:

请使用这个:

 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))
        }

它将被执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多