欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中ListView + CheckBox实现单选、多选效果

程序员文章站 2023-12-21 10:45:22
还是先来看看是不是你想要的效果: 不废话,直接上代码,很简单,代码里都有注释 1 单选 public class singleactivity exten...

还是先来看看是不是你想要的效果:

Android中ListView + CheckBox实现单选、多选效果

不废话,直接上代码,很简单,代码里都有注释

1 单选

public class singleactivity extends appcompatactivity {
 private listview listview;
 private arraylist<string> groups;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_single);
  listview = (listview) this.findviewbyid(r.id.lvgroup);
  groups = new arraylist<string>();
  groups.add("11");
  groups.add("22");
  groups.add("33");
  groups.add("44");
  groups.add("55");
  groups.add("66");
  groups.add("77");
  adapterview.onitemclicklistener listitemclicklistener = new adapterview.onitemclicklistener() {
   @override
   public void onitemclick(adapterview<?> parent, view view, int position, long id) {
    // 取得viewholder对象,这样就省去了通过层层的findviewbyid去实例化我们需要的cb实例的步骤
    singleadapter.viewholder viewholder = (singleadapter.viewholder) view.gettag();
    viewholder.cb.toggle();// 把checkbox的选中状态改为当前状态的反,gridview确保是单一选中
   }
  };
  singleadapter adapter = new singleadapter(this, groups);
  listview.setadapter(adapter);
  listview.setonitemclicklistener(listitemclicklistener);
 }
}

adapter:

public class singleadapter extends baseadapter {
 private activity activity;//上下文
 private arraylist<string> list;
 private layoutinflater inflater = null;//导入布局
 private int temp = -1;
 public singleadapter(activity context, arraylist<string> list) {
  this.activity = context;
  this.list = list;
  inflater = layoutinflater.from(context);
 }
 @override
 public int getcount() {
  return list.size();
 }
 @override
 public object getitem(int position) {
  return list.get(position);
 }
 @override
 public long getitemid(int position) {
  return position;
 }
 //listview每显示一行数据,该函数就执行一次
 @override
 public view getview(int position, view convertview, viewgroup parent) {
  viewholder holder = null;
  if (convertview == null) {//当第一次加载listview控件时 convertview为空
   convertview = inflater.inflate(r.layout.group_item_view, null);//所以当listview控件没有滑动时都会执行这条语句
   holder = new viewholder();
   holder.tv = (textview) convertview.findviewbyid(r.id.item_tv);
   holder.cb = (checkbox) convertview.findviewbyid(r.id.item_cb);
   convertview.settag(holder);//为view设置标签
  } else {//取出holder
   holder = (viewholder) convertview.gettag();
  }
  //设置list的textview显示
  holder.tv.settextcolor(color.white);
  holder.tv.settext(list.get(position));
  // 根据isselected来设置checkbox的选中状况
  holder.cb.setid(position);//对checkbox的id进行重新设置为当前的position
  holder.cb.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
   @override
   public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) {
    if (ischecked) {//实现checkbox的单选功能,同样适用于radiobutton
     if (temp != -1) {
      //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
      checkbox tempcheckbox = (checkbox) activity.findviewbyid(temp);
      if (tempcheckbox != null)
       tempcheckbox.setchecked(false);
     }
     temp = compoundbutton.getid();//保存当前选中的checkbox的id值
    }
   }
  });
  if (position == temp)//比对position和当前的temp是否一致
   holder.cb.setchecked(true);
  else
   holder.cb.setchecked(false);
  return convertview;
 }
 public static class viewholder {
  textview tv;
  checkbox cb;
 }
}

多选:

public class mulactivity extends appcompatactivity {
 private listview listview;
 private arraylist<string> groups;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_mul);
  listview = (listview) this.findviewbyid(r.id.list);
  groups = new arraylist<>();
  groups.add("11");
  groups.add("22");
  groups.add("33");
  groups.add("44");
  groups.add("55");
  groups.add("66");
  groups.add("77");
  adapterview.onitemclicklistener listitemclicklistener = new adapterview.onitemclicklistener() {
   @override
   public void onitemclick(adapterview<?> parent, view view, int position,
         long id) {
    // 取得viewholder对象,这样就省去了通过层层的findviewbyid去实例化我们需要的cb实例的步骤
    muladapter.viewholder viewholder = (muladapter.viewholder) view.gettag();
    viewholder.cb.toggle();// 把checkbox的选中状态改为当前状态的反,gridview确保是单一选中
    muladapter.getisselected().put(position, viewholder.cb.ischecked());//将checkbox的选中状况记录下来
   }
  };
  muladapter adapter = new muladapter(this, groups);
  listview.setadapter(adapter);
  listview.setonitemclicklistener(listitemclicklistener);
 }
}

adapter:

public class muladapter extends baseadapter {
 private context context;//上下文
 private arraylist<string> list;
 //控制checkbox选中情况
 private static hashmap<integer, boolean> isselected;
 private layoutinflater inflater = null;//导入布局
 public muladapter(context context, arraylist<string> list) {
  this.context = context;
  this.list = list;
  inflater = layoutinflater.from(context);
  isselected = new hashmap<integer, boolean>();
  initdata();
 }
 private void initdata() {//初始化isselected的数据
  for (int i = 0; i < list.size(); i++) {
   getisselected().put(i, false);
  }
 }
 @override
 public int getcount() {
  return list.size();
 }
 @override
 public object getitem(int position) {
  return list.get(position);
 }
 @override
 public long getitemid(int position) {
  return position;
 }
 //listview每显示一行数据,该函数就执行一次
 @override
 public view getview(int position, view convertview, viewgroup parent) {
  viewholder holder = null;
  if (convertview == null) {//当第一次加载listview控件时 convertview为空
   convertview = inflater.inflate(r.layout.group_item_view, null);//所以当listview控件没有滑动时都会执行这条语句
   holder = new viewholder();
   holder.tv = (textview) convertview.findviewbyid(r.id.item_tv);
   holder.cb = (checkbox) convertview.findviewbyid(r.id.item_cb);
   convertview.settag(holder);//为view设置标签
  } else {//取出holder
   holder = (viewholder) convertview.gettag();//the object stored in this view as a tag
  }
  //设置list的textview显示
  holder.tv.settextcolor(color.white);
  holder.tv.settext(list.get(position));
  // 根据isselected来设置checkbox的选中状况
  holder.cb.setchecked(getisselected().get(position));
  return convertview;
 }
 public static class viewholder {
  textview tv;
  checkbox cb;
 }
 public static hashmap<integer, boolean> getisselected() {
  return isselected;
 }
 public static void setisselected(hashmap<integer, boolean> isselected) {
  muladapter.isselected = isselected;
 }
}

以上所述是小编给大家介绍的android中listview + checkbox实现单选、多选效果,希望对大家有所帮助

上一篇:

下一篇: