In Android RecyclerView How to change the color of Alternate rows

前端 未结 6 2164
南笙
南笙 2021-02-05 11:45

I am new in android , recently I have learned recyclerview and i want to change the color of rows.

Example: I have 10 rows and I want to change color like

6条回答
  •  广开言路
    2021-02-05 12:16

    With CardView in Kotlin

     internal fun bind(d: Detalle, position: Int, listener: OnItemClickListener) {
    
            if (position % 2 == 1) {
                cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.blue_logo))
            } else {
                cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.colorWhite))
            }
    

    Multiples Colors

    when {
    p % 4 == 0 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.yellow)) 
    p % 4 == 1 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.green)) 
    p % 4 == 2 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.blue)) 
    p % 4 == 3 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.red)) 
    }
    

提交回复
热议问题