当前位置: 代码迷 >> Android >> listview利用onitemclick来对选中item进行变色有关问题
  详细解决方案

listview利用onitemclick来对选中item进行变色有关问题

热度:81   发布时间:2016-05-01 21:24:07.0
listview利用onitemclick来对选中item进行变色问题
在用SDK1.5进行开发时,利用下面的语句,对选中的item进行变色

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

filename = data[data.length - arg2 - 1].toString();
filepath = voicedirpath + "/" + filename;

if ((data.length - arg2 - 1) != priItem) {
if (priView == null) { //当前未选中item时
arg1.setBackgroundResource(R.drawable.popup_edit_hl);//将选中item背景设为高亮
priItem = data.length - 1 - arg2;
priView = arg1; //记录选中的项信息
}
  else { //当已有选中的item,再点击其它item时
priView.setBackgroundResource(R.drawable.background_listitem);//将原选中
  item背景改为默认背景
arg1.setBackgroundResource(R.drawable.popup_edit_hl);//将选中item背景设为高亮
priItem = data.length - 1 - arg2;
priView = arg1; /记录选中的项信息
}
} else {//当点击的是当前选中的item时
arg1.setBackgroundResource(R.drawable.background_listitem);//将item背景重新设为默认背景
priItem = -1;
filepath = null;
filename = null;
priView = null;
}

}

在1.5真机和模拟器上都可以正常使用

但在1.6以上的机子上就出现问题了,第一次单击选中时被设为高亮的item不是被点的那一项,很奇怪的问题
按理说SDK应该是向下兼容的,在1.5上能用,在1.6以上也能用才对,不知道这个问题该怎么解决
求指点,在线急等

------解决方案--------------------
...这个兼容不太一定,1.5的方法有一些到了1.6就废弃了,1.6的部分方法到2.1也被废弃了。但是2.1的方法有些是1.6和1.5不支持的。
------解决方案--------------------
试试selector 吧

例子
http://blog.sina.com.cn/s/blog_6091867a0100jrkq.html

------解决方案--------------------
在layout/*.xml
  相关解决方案