[英]Changing custom List view row background color
I have an application with a custom list view which has a textview and an image(delete), when I click the image the background color of that row should change and when I click the same image again its background should change to default color. 我有一个带有自定义列表视图的应用程序,该列表视图具有textview和image(delete),当我单击该图像时,该行的背景色应更改,而当我再次单击同一图像时,其背景应更改为默认颜色。 I'm able to change the background color but only once, Im not able to change it twice, I mean Im not able to revert back to its default color.
我可以更改背景颜色,但是只能更改一次,我不能更改两次,我的意思是我无法恢复为默认颜色。
Here is my code ... 这是我的代码...
CustomListView.java CustomListView.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;
}
And one more problem is, that background color is not the color I mentioned in color.xml, I tested by putting different colors but when I click the image that color is changing to only one particular color. 还有一个问题是,背景色不是我在color.xml中提到的颜色,我通过放置不同的颜色进行了测试,但是当我单击图像时,该颜色仅变为一种特定的颜色。
So, to put it simple, I need to change the background color of listview row when I click the image and revert back to the default color when I click it again. 因此,为了简单起见,我需要在单击图像时更改listview行的背景颜色,并在再次单击它时恢复为默认颜色。
Any kind of help is much appreciated. 任何帮助都将不胜感激。 Thanks !
谢谢 !
I think the color may already changed, but you make mistake at setBackgroundColor(), so it seem like the color is not changing. 我认为颜色可能已经更改,但是您在setBackgroundColor()上犯了错误,因此似乎颜色没有更改。
For the color problem try using this code 对于颜色问题,请尝试使用此代码
clickView.setBackgroundColor(context.getResources().getColor(R.color.list_row_bg));
You have to pass Context object when instantiate adapter. 实例化适配器时,您必须传递Context对象。
or you can use something like 或者您可以使用类似
layout.setBackgroundColor(0xFFFFFFFF);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.