今天 在eoe.Android 论坛 有人提出这个问题
开始觉得很简单 可后来一想 牵出一点东西 既然涉及给item上背景图 那么应该如何去除 及刷新呢? 故深入之 现告诉大家应该怎么做
现有ListView 其id="R.id.list" 该代码仅涉及背景色相关代码 其他 比如:其与Adapter绑定 等代码 略
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
for(int i=0;i<list.getCount();i++){
arg0.getChildAt(i).setBackgroundDrawable(null);
}
arg1.setBackgroundColor(Color.RED);
}
});
现细说之:
1. onItemClick() 用于跟踪用户选项单击
2. 除去上次选项背景色 我的办法:所有选项的背景色都置空
for(int i=0;i<list.getCount();i++){
arg0.getChildAt(i).setBackgroundDrawable(null);
}
其实 该办法方法很多 可以用一个int变量保存上次选中的id 但是这又会引出一个问题 就是:第一个被单击 等等问题 所以使用这个办法 就是: ListView 所有的item 的背景色都置空
3. 设置选中的item 背景色为 红色 Color.RED
arg1.setBackgroundColor(Color.RED);
4. 效果图就免了 还要启动emulator 那个时间 心痛 据说Google 要改进这点了 这对于我们这些没有真机的人是个福音~~~~~~~~~~
本文介绍了一种在Android ListView中实现点击项高亮显示并取消其他项高亮的方法。通过遍历ListView的所有子项并移除背景Drawable来清除之前的选择状态,然后将点击项的背景颜色设置为红色。
3154

被折叠的 条评论
为什么被折叠?



