在Android RecyclerView中如何更改备用行的颜色 [英] In Android RecyclerView How to change the color of Alternate rows

查看:249
本文介绍了在Android RecyclerView中如何更改备用行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,最近我学会了recyclerview,我想更改行的颜色.

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

示例:我有10行,我想更改颜色,例如5行蓝色和5行红色.备用行颜色应该是这样的.

Example: I have 10 rows and I want to change color like 5 rows blue and 5 rows red.Alternate rows color should be like this.

我必须从这里通过Adapter或MainActivity进行更改. 请帮助我

From where I have to change this by Adapter or by MainActivity. Please help me

推荐答案

您可以通过在Adapter类上添加以下代码来更改备用行的颜色. 您还可以使用此代码更改行的图像.

You can change the color of alternate row by adding the following code on your Adapter class. You can also change the images of your row by using this code.

将此代码放在适配器类的OnBindViewHolder中.

Put this code inside OnBindViewHolder in Adapter Class.

 if(position %2 == 1)
    {
        holder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    else
    {
       holder.itemView.setBackgroundColor(Color.parseColor("#FFFAF8FD"));
       //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFAF8FD"));
    }

这篇关于在Android RecyclerView中如何更改备用行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆