单击一次后如何更改列表视图的背景颜色,再次单击时我需要更改背景颜色而不使用选择器。当我选择每个项目时,我一直在改变背景。但是当我再次点击时项目我需要改变背景颜色为red.how我能给出条件。我会在这里发布我的代码。请给出建议。请帮助......
公共类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);
}
}
}
});
}
}
答案 0 :(得分:0)
使用此编辑的代码。
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);
}
}
}
});
}
}
这会对你有帮助。