Android ListItem更改背景色
2010-08-21 03:21:00 来源:WEB开发网return false;
}
};
//listview的item点击事件
private OnItemClickListener itemClicklistener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Log.i(TAG, "**************onItemClick arg0 is " + arg0.getClass().getName());
Log.i(TAG, "**************onItemClick arg1 is " + arg1.getId());
Log.i(TAG, "**************onItemClick arg2 is " + arg2);
Log.i(TAG, "**************onItemClick arg3 is " + arg3);
itemBackChanged(arg1);
position = arg2;
}
};
//改变listitem的背景色
private void itemBackChanged(View view) {
//改变当前点击的listitem中textview的字体颜色
TextView contactName = (TextView)view.findViewById(R.id.contactName);
TextView contactTel = (TextView)view.findViewById(R.id.contactTel);
contactName.setTextColor(Color.WHITE);
contactTel.setTextColor(Color.WHITE);
if(itemView == null) itemView = view;
if(itemView != view) {
//改变上次点击点击的listitem中textview的字体颜色
contactName = (TextView)itemView.findViewById(R.id.contactName);
contactTel = (TextView)itemView.findViewById(R.id.contactTel);
contactName.setTextColor(Color.BLACK);
contactTel.setTextColor(Color.BLACK);
}
itemView.setBackgroundColor(android.R.color.transparent);//将上次点击的listitem的背景色设置成透明
view.setBackgroundResource(R.color.list_item_focus);//设置当前点击的listitem的背景色
itemView = view;
}
6、如果是图片的话,第三部可以跳过,第5步中view.setBackgroundResource(R.color.list_item_focus)改成view.setBackgroundResource(R.drawable.select_bar);就行了