在自定义适配器中,我在 getView 方法中执行了以下操作:
if ( ual.getTransactionCategory() == TransactionCategory.WARNING ){
convertView.setBackgroundColor(convertView.getResources().getColor(R.color.yellow_warning));
}
...
return convertView;
通过在ListView中设置适配器,我成功地将警告项目显示为黄色背景。现在,我的问题是我想在单击它时将警告项的背景设置为白色。所以在ListView的 onItemClick 中,我做了:
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
view.setBackgroundColor(getResources().getColor(R.color.white));
}
但它并没有改变背景。
相反,如果我没有在 getView 中设置背景,我可以在 onItemClick 即可。 有什么建议吗?
答案 0 :(得分:0)
您需要使用state drawable resource。在此drawable中,您可以定义不同状态的颜色。然后,在构建视图时,请执行以下操作:
if ( ual.getTransactionCategory() == TransactionCategory.WARNING ){
convertView. setBackgroundResource(R.drawable.your_state_drawable);
}
...
return convertView;
答案 1 :(得分:0)
您已从视图中引用内部视图。以下应该有效。
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
ImageView iv = (ImageView) v.findViewById(R.id.menu_icon); //What ever your inline view is
iv.setBackgroundColor(getResources().getColor(R.color.white));
}