列表查看项目背景颜色

问题描述:

你好我怎样才能设置项目班轮布局背景颜色从这个数组,它只有3种颜色,位置一个颜色一个,位置两个颜色两个,位置三个颜色三个,然后位置一个颜色一个和等..列表查看项目背景颜色

列表视图的位置

int[] androidColors = context.getResources().getIntArray(R.array.randomColor); 
    viewHolder.linearLayout.setBackgroundColor(androidColors[position]); 
+0

我认为模运算符('位置%3')是你在找什么。 –

+0

@ vishal-wadhwa谢谢你,我想要什么 –

你可以尝试创建自己的自定义适配器和落实getView功能是这样的:

public View getView (int position, View convertView, ViewGroup parent){ 
if(convertView == null){ 
    //We must create a View: 
    convertView = inflater.inflate(R.layout.my_list_item, parent, false); 
} 
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view. 
//or an image on an ImageView. 
return convertView; 

}

尝试看看这个帖子: Change background colour of current listview item in adapter getView method