[英]how to change the background color of a listview when clicked once and when clicked again i need to change the background color without using selector
如何在一次单击和再次单击时更改列表视图的背景颜色,我需要不使用选择器来更改背景颜色。我已经在选择每个项目时更改了背景。但是当我再次单击该项目时将背景颜色更改为红色。我如何给出条件。我将在此处发布我的代码。请提供建议。请帮助...
公共类ProvierActivity扩展了Activity {
private String text[] = { "BroadStripe-Cable (Seattle)",
"BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
"Comcast king county south)", "BroadStripe-Cable (Seattle)",
"Comcast king county south", "BroadStripe-Digital (Seattle)",
"BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
"Comcast king county south" };
ImageView icon;
public static int selectedRow;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView list = (ListView) findViewById(R.id.listview_id);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
R.id.title, text));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg3) {
for (int i = 0; i < adapter.getChildCount(); i++) {
if (i == position) {
adapter.getChildAt(i).setBackgroundColor(Color.BLUE);
} else {
adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
}
}
}
});
}
}
使用此编辑后的代码。
public static int selectedRow;
/** Called when the activity is first created. */
private int prePos=-1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView list = (ListView) findViewById(R.id.listview_id);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
R.id.title, text));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg3) {
for (int i = 0; i < adapter.getChildCount(); i++) {
if (i == position) {
if(position!=prePos){
adapter.getChildAt(i).setBackgroundColor(Color.BLUE);
prePos=position;
}else{
adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
prePos=-1;
}
}else{
adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
}
}
}
});
}
}
这将为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.