2017-10-14 179 views
0

你好我怎樣才能設置項目班輪佈局背景顏色從這個數組,它只有3種顏色,位置一個顏色一個,位置兩個顏色兩個,位置三個顏色三個,然後位置一個顏色一個和等..列表查看項目背景顏色

列表視圖的位置

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

我認爲模運算符('位置%3')是你在找什麼。 –

+0

@ vishal-wadhwa謝謝你,我想要什麼 –

回答

0

你可以嘗試創建自己的自定義適配器和落實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