0
我已經實現了列表項的自定義適配器。我有兩個要求列表項行顏色問題android
1)列表項目應該有替代顏色。要實現這一點,我有下面的代碼
private final int[] bgColors = new int[] {R.color.list_bg_1, R.color.list_bg_2};
int colorPosition = position % bgColors.length;
convertView.setBackgroundResource(bgColors[colorPosition]);
2)當你點擊列表項,更應突出
Drawable selectedBackground;
selectedBackground = context.getResources().getDrawable(R.color.backgroundColor);
if (selectedPos == position) {
convertView.setBackgroundDrawable(selectedBackground);
} else {
convertView.setBackgroundDrawable(null);
}
// this method is called in onItemClick in Activity.
public void setSelectedPosition(int pos){
selectedPos = pos;
notifyDataSetChanged();
}
問題:當我把兩個碼,任一功能不起作用。我怎樣才能確保這兩個功能與上面的代碼一起工作?
看到這個[SO答案](http://stackoverflow.com/questions/2050533/list-items-with-alt ernating-顏色) – codeMagic 2013-04-25 02:22:42