1. 程式人生 > >ListView隔行變色

ListView隔行變色

ListView 實現斑馬線效果

ListView是以條目方式展示資料的,有時候專案展示資料時需要隔行變色實現斑馬線效果.

在Adapter中定義color陣列(陣列中的值為你要隔行變色的值)

int[] colors = new int[] { R.color.inverst_project_bg1, R.color.white };

在Adapter中的getView對資料做處理

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
arg1 = getLayoutInflater().inflate(R.layout.inverstproject_item, null);
int colorPos = arg0 % colors.length; // 計算該條目是否單雙


arg1.setBackgroundResource(colors[arg0%2]);
return arg1;
}