我有一个带有自定义列表视图的应用程序,它有一个文本视图和一个图像(删除),当我单击图像时,该行的背景颜色应该改变,当我再次单击同一个图像时,它的背景应该改变为默认颜色。我可以更改背景颜色,但只能更改一次,我无法更改两次,我的意思是我无法恢复为默认颜色。
这是我的代码...
自定义列表视图.java
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
DataFields rowItems = (DataFields) getItem(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.home_field_row, null);
holder = new ViewHolder();
holder.dataFields = items.get(position);
holder.mName = (TextView) convertView
.findViewById(R.id.hmFieldName);
holder.mDeleteImage = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteImage);
holder.mDeleteCheck = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteCheck);
holder.mDeleteMainRL = (RelativeLayout) convertView
.findViewById(R.id.hmFieldMainRL);
holder.mDeleteImage.setTag(position);
final View clickView = convertView;
holder.mDeleteImage
.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
int status = 0;
HomeActivity.mDeleteFieldLL
.setVisibility(View.VISIBLE);
HomeActivity.hmAddField
.setVisibility(View.INVISIBLE);
holder.dataFields = items.get(position);
if (mFieldId.size() == 0) {
mFieldId.add(holder.dataFields);
++count;
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
clickView.setBackgroundColor(R.color.list_row_bg);
} else {
for (int i = 0; i < mFieldId.size(); i++) {
if (mFieldId.get(i).getId() == holder.dataFields
.getId()) {
status = 1;
}
}
if (status == 0) {
mFieldId.add(holder.dataFields);
++count;
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
clickView.setBackgroundColor(R.color.list_row_bg);
} else if (status == 1) {
mFieldId.remove(holder.dataFields);
--count;
if (count < 0)
count = 0;
clickView.setBackgroundColor(R.color.list_row_bg_default); //doesnt changes back to default color
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
}
}
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mName.setText(rowItems.getName());
return convertView;
}
还有一个问题是,背景颜色不是我在 color.xml 中提到的颜色,我通过放置不同的颜色进行了测试,但是当我单击图像时,颜色只会变成一种特定的颜色。
因此,简单地说,我需要在单击图像时更改 listview 行的背景颜色,并在再次单击时恢复为默认颜色。
非常感谢任何形式的帮助。谢谢 !