你能帮我找出如何让我的 ListView 子项目的每个项目都有不同的颜色吗?就像第一个是白色的,第二个是黑色的,它会为每个新的子项目迭代这个颜色。
可以做到吗?如果是的话,你能指导我去哪里做这件事吗?
提前致谢!
您与 List.xml 相关的 Activity 应该从 ListActivity 类扩展
在 onCreate 方法里面
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();//RowData should get you, your data
CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
R.id.title, data);
setListAdapter(adapter);
添加一个新的 CustomAdapter 类
public class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(position%2==0)
convertView.setBackgroundColor(color.darker_gray);
}