如何根据特定条件给出不同样式的列表项目?
问题描述:
我正在用适配器构建字符串列表,我如何根据特定条件将不同的布局属性应用于这些项目?如何根据特定条件给出不同样式的列表项目?
例如,假设您需要检查列表项的文本是否包含特定的单词,然后基于测试的结果,您想要增加或减少其边距(如果这样可以有道理:D)。
答
创建如下所示的适配器。这里只背景颜色改变,尝试改变布局有根据乌拉圭回合的条件....
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
noob问题:如果我的活动扩展的ListView,我还可以做这个工作? – user1301428
好的,我明白了。无论如何,另一个问题是:如果我的状况涉及列表项的部分文本会怎么样?我编辑了这个问题,使这一点更清晰。 – user1301428