当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程记录ListView标记行状态的方法

Android编程记录ListView标记行状态的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

兔年男宝宝起名大全,善男信女 步微澜,阿鲁科尔沁旗绿源

本文实例讲述了android编程记录listview标记行状态的方法。分享给大家供大家参考,具体如下:

小demo无什么特别之处,最特别的就是尽量少用notifydatasetchanged,开销太大了,当然使用是会省不少工的,不过有时候还是会遇到别的问题的,项目经验表示会有这个可能性的,废话不多少了,直接上关键代码。

@override
public void onitemclick(adapterview<?> adapterview, view view, int pos, long arg3) {
  /**
   * 在listview中,使用getchildat(index)的取值,只能是当前可见区域(列表可滚动)的子项!
  1、所以如果想获取前部的将会出现返回null值问题;
  2、getchildcount跟getcount获取的值将会不一样(数量多时);
  3、如果使用了getchildat(index).findviewbyid(...)设置值的话,滚动列表时值就会改变了。
  需要使用getfirstvisibleposition()获得第一个可见的位置,在用当前的position-getfirstvisibleposition(),再用getchildat取值!
   * */
  int now_pos = pos - adapterview.getfirstvisibleposition();
  view v = adapterview.getchildat(now_pos);
  imageview imageview = (imageview) v.findviewbyid(r.id.image);
  if (imageview.getvisibility() == view.visible) {
   imageview.setvisibility(view.gone);
   adapter.setstate(pos,nonbiaozhi);
  }else{
   imageview.setvisibility(view.visible);
   adapter.setstate(pos,biaozhi);
   // getview調用,不過非常耗費性能
//   adapter.notifydatasetchanged();
   // 不使用notifydatasetchanged,getview不會刷新,提高效率
   refreshlistview(pos, imageview);
  }
}
public void refreshlistview(int pos, imageview imageview) {
  if(pre != pos && !hasmap.isempty()){
   imageview image = (imageview) hasmap.get(pre);
   image.setvisibility(view.gone);
   hasmap.remove(pre);
  }
  pre = pos;
  hasmap.put(pre, imageview);
}

上面的是在activity的方法片段,下面的是baseadapter的方法片段:

// 設置標記
public void setstate(int pos,int state){
  if(state == mainactivity.biaozhi){
   // 每次點擊都清空列表,保持唯一選擇性
   map.clear();
   map.put(pos, 1);
  }else{
   map.remove(pos);
  }
}
// 檢測標記
public boolean getstate(int p){
  if (!map.isempty() && map.containskey(p)) {
   return true;
  }else
   return false;
}

getview代码:

// 固定显示标记的行
if (getstate(position)) {
 holder.imageview.setvisibility(view.visible);
}else
 holder.imageview.setvisibility(view.gone);

附上项目下载,自己运行就会得到标题的效果。完整实例代码代码点击此处。
希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网