Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码

Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码,第1张

概述最近在项目开发中,由于项目的需求要实现一些列表的单选,多选,全选,批量输入之类的功能,其实功能的实现倒不是很复杂,需求中也没有涉及到复杂的动画什么之类,主要是解决列表数据复用的问题,解决好这个就可以了

最近在项目开发中,由于项目的需求要实现一些列表的单选,多选,全选,批量输入之类的功能,其实功能的实现倒不是很复杂,需求中也没有涉及到复杂的动画什么之类,主要是解决列表数据复用的问题,解决好这个就可以了。下面是最近项目中涉及到的一些:

ListvIEw实现多选、全选、取消全选:

下面是适配器,一开始在适配器的构造函数中,对数据进行初始化,同时定义一个集合用于管理ListvIEw的点击;

class BatchAdpter extends BaseAdapter { private HashMap<Integer,Boolean> isSelected; private List<DtGzsCustomer> List; private Context context; @Suppresslint("UseSparseArrays") public BatchAdpter(List<DtGzsCustomer> List,Context context) { this.context=context; this.List = new ArrayList<DtGzsCustomer>(); if (List != null) { this.List.addAll(List); } isSelected = new HashMap<Integer,Boolean>(); initDate(false); } // 初始化isSelected的数据 private voID initDate(boolean bool) { for (int i = 0; i < List.size(); i++) { DtGzsCustomer dtGzsCustomer = List.get(i); if (bool) {    datas.add(dtGzsCustomer.thread_ID); } else {  datas.remove(dtGzsCustomer.thread_ID); } getIsSelected().put(i,bool); } } public HashMap<Integer,Boolean> getIsSelected() { return isSelected; } public voID setIsSelected(HashMap<Integer,Boolean> isSelected) { this.isSelected = isSelected; } public voID nodfiyData(List<DtGzsCustomer> List) { if (List != null) { this.List.clear(); this.List.addAll(List); } notifyDataSetChanged(); } @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; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder holder; if (convertVIEw == null) { holder = new VIEwHolder(); convertVIEw = VIEw.inflate(context,R.layout.no_contact_ListvIEw_item,null); holder.name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.name); holder.sex = (TextVIEw) convertVIEw.findVIEwByID(R.ID.sex); holder.popuse = (TextVIEw) convertVIEw.findVIEwByID(R.ID.popuse); holder.tv_phone = (TextVIEw) convertVIEw.findVIEwByID(R.ID.tv_phone); holder.allocation = (TextVIEw) convertVIEw.findVIEwByID(R.ID.allocation); holder.time = (TextVIEw) convertVIEw.findVIEwByID(R.ID.time); holder.btn_dis = (button) convertVIEw.findVIEwByID(R.ID.btn_dis); holder.btn_allot = (button) convertVIEw.findVIEwByID(R.ID.btn_allot); holder.btn_telephone = (button) convertVIEw.findVIEwByID(R.ID.btn_telephone); holder.btn_follow = (button) convertVIEw.findVIEwByID(R.ID.btn_follow); holder.cb = (CheckBox) convertVIEw.findVIEwByID(R.ID.cb); holder.allocation.setVisibility(VIEw.INVISIBLE); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } DtGzsCustomer data = List.get(position); SalesTools.setTextVIEwText(holder.name,data.p_customer_name); SalesTools.setTextVIEwText(holder.sex,data.gender); SalesTools.setTextVIEwText(holder.popuse,data.purpose_serIEs); SalesTools.setTextVIEwText(holder.tv_phone,data.mobile_no); SalesTools.setTextVIEwText(holder.allocation,data.thread_contact_state); SalesTools.setTextVIEwText(holder.time,data.thread_create_date); holder.btn_dis.setVisibility(VIEw.INVISIBLE); holder.btn_allot.setVisibility(VIEw.INVISIBLE); holder.btn_telephone.setVisibility(VIEw.INVISIBLE); holder.btn_follow.setVisibility(VIEw.INVISIBLE); holder.cb.setVisibility(VIEw.VISIBLE); HashMap<Integer,Boolean> isSelected2 = getIsSelected(); Boolean boolean1 = isSelected2.get(position); if (boolean1 != null) { holder.cb.setChecked(boolean1); } // 初始化的时候做处理,如果管理标记的集合中没有该pos则设为false如果有则设为true if (adapter != null) { HashMap<Integer,Boolean> isSelected = adapter.getIsSelected(); if (isSelected != null && isSelected.containsKey(position)) {  holder.cb.setChecked(isSelected.get(position)); } else {  holder.cb.setChecked(false); } } return convertVIEw; } } class VIEwHolder { TextVIEw name,sex,popuse,tv_phone,allocation,time; button btn_dis,btn_allot,btn_telephone,btn_follow; CheckBox cb; }

这里是全选的方法:

// 全选 private voID choiceAll() { adapter.initDate(true); // 数量设为List的长度 checkNum = List.size(); // 刷新ListvIEw和TextVIEw的显示 cb_all.setTextcolor(color.parsecolor("#0097e0")); dataChanged(); }

这里是条目点击事件:

@OverrIDe public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) { DtGzsCustomer dtGzsCustomer = List.get(position); // 取得VIEwHolder对象,这样就省去了通过层层的findVIEwByID去实例化我们需要的cb实例的步骤 VIEwHolder holder = (VIEwHolder) vIEw.getTag(); // 改变CheckBox的状态 holder.cb.toggle(); // 将CheckBox的选中状况记录下来 adapter.getIsSelected().put(position,holder.cb.isChecked()); // 调整选定条目 if (holder.cb.isChecked() == true) { checkNum++; if (checkNum==List.size()) { cb_all.setChecked(true); cb_all.setTextcolor(color.parsecolor("#0097e0")); } datas.add(dtGzsCustomer.thread_ID); } else { cb_all.setChecked(false); cb_all.setTextcolor(color.parsecolor("#c0c0c0")); checkNum--; datas.remove(dtGzsCustomer.thread_ID); } dataChanged(); }

下面是ListvIEw和radionbutton实现单个条目单选,整个列表多线:

class CheckAdapter extends BaseAdapter { private Context mContext; private List<DtGzsSchedule> List; public CheckAdapter(Context context,List<DtGzsSchedule> List) { this.mContext = context; this.List=List; } @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; } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { VIEwHolder holder; if (convertVIEw==null) { holder=new VIEwHolder(); convertVIEw=LayoutInflater.from(mContext).inflate(R.layout.check_ListvIEw_item,parent,false); holder.sale_name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.sale_name); holder.sale_branch = (TextVIEw) convertVIEw.findVIEwByID(R.ID.sale_branch); holder.scb = (Radiobutton) convertVIEw.findVIEwByID(R.ID.scb); holder.scc = (Radiobutton) convertVIEw.findVIEwByID(R.ID.scc); holder.scd = (Radiobutton) convertVIEw.findVIEwByID(R.ID.scd); holder.sce = (Radiobutton) convertVIEw.findVIEwByID(R.ID.sce); convertVIEw.setTag(holder); }else{ holder=(VIEwHolder) convertVIEw.getTag(); } final DtGzsSchedule dtGzsSchedule = List.get(position); OnCheckedchangelistener Listener = new OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {  String choice = buttonVIEw.getText().toString();  if (choice.equals("到岗")) {  if (isChecked == true) {  dtGzsSchedule.check_type = "0";  setActualNum();  }  } else {  if (choice.equals("迟到")) {  if (isChecked == true) {  dtGzsSchedule.check_type = "1";  setActualNum();  }  } else if (choice.equals("休假")) {  if (isChecked == true) {  dtGzsSchedule.check_type = "2";  setActualNum();  }  } else if (choice.equals("旷工")) {  if (isChecked == true) {  dtGzsSchedule.check_type = "3";  setActualNum();  }  }  } } }; holder.sce.setonCheckedchangelistener(Listener); holder.scd.setonCheckedchangelistener(Listener); holder.scc.setonCheckedchangelistener(Listener); holder.scb.setonCheckedchangelistener(Listener); holder.sale_name.setText("" + dtGzsSchedule.sales_consultant_name); holder.sale_branch.setText("" + dtGzsSchedule.sales_branch); String check_type = dtGzsSchedule.check_type; if (check_type.equals("0")) {// 到岗 dtGzsSchedule.scbChecked = true; dtGzsSchedule.sccChecked = false; dtGzsSchedule.scdChecked = false; dtGzsSchedule.sceChecked = false; } else if (check_type.equals("1")) {// 迟到 dtGzsSchedule.scbChecked = false; dtGzsSchedule.sccChecked = true; dtGzsSchedule.scdChecked = false; dtGzsSchedule.sceChecked = false; } else if (check_type.equals("2")) {// 旷工 dtGzsSchedule.scbChecked = false; dtGzsSchedule.sccChecked = false; dtGzsSchedule.scdChecked = true; dtGzsSchedule.sceChecked = false; } else if (check_type.equals("3")) {// 休假 dtGzsSchedule.scbChecked = false; dtGzsSchedule.sccChecked = false; dtGzsSchedule.scdChecked = false; dtGzsSchedule.sceChecked = true; } holder.scb.setChecked(dtGzsSchedule.scbChecked); holder.scc.setChecked(dtGzsSchedule.sccChecked); holder.scd.setChecked(dtGzsSchedule.scdChecked); holder.sce.setChecked(dtGzsSchedule.sceChecked); return convertVIEw; } } class VIEwHolder{ TextVIEw sale_name,sale_branch; Radiobutton scb,scc,scd,sce; }

ExpandableListVIEw实现子条目单选:

class PinnedheaderExpandableAdapter extends Baseexpandablelistadapter implements headerAdapter { private Context context; private PinnedheaderExpandableListVIEw ListVIEw; private LayoutInflater inflater; private Map<String,List<DtGzsCustomer>> map; private List<String> parentList; @Suppresslint("UseSparseArrays") public PinnedheaderExpandableAdapter(List<String> parentList,Map<String,List<DtGzsCustomer>> map,Context context,PinnedheaderExpandableListVIEw ListVIEw) { this.context = context; this.ListVIEw = ListVIEw; inflater = LayoutInflater.from(this.context); this.map = new HashMap<String,List<DtGzsCustomer>>(); if (map != null) { this.map.putAll(map); } this.parentList = new ArrayList<String>(); if (parentList != null) { this.parentList.addAll(parentList); } } public voID notifyMap(Map<String,List<DtGzsCustomer>> map) { if (map != null) { this.map.clear(); this.map.putAll(map); } notifyDataSetChanged(); } public voID notifyParent(List<String> parentList) { if (parentList != null) { this.parentList.clear(); this.parentList.addAll(parentList); } notifyDataSetChanged(); } @OverrIDe public int getChildrenCount(int groupposition) { if (groupposition != -1) { String key = parentList.get(groupposition); return map.get(key).size(); } else { return 0; } } @OverrIDe public Object getChild(int groupposition,int childposition) { String key = parentList.get(groupposition);// 根据组名位置的值作为map的key去获取value DtGzsCustomer dtGzsCustomer = map.get(key).get(childposition); return dtGzsCustomer.sales_consultant_name; } @OverrIDe public long getChildID(int groupposition,int childposition) { return childposition; } @OverrIDe public VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,VIEwGroup parent) { convertVIEw = inflater.inflate(R.layout.child,null); TextVIEw text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.childto); ImageVIEw iv = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.iv); // 判断item的位置是否相同,如相同,则表示为选中状态,更改其背景颜色,如不相同,则设置背景色为白色 if (group_groupID == groupposition && child_childID == childposition) { iv.setimageResource(R.drawable.login_check); } else { iv.setimageResource(R.drawable.login_uncheck); } String key = parentList.get(groupposition); List<DtGzsCustomer> List = map.get(key); DtGzsCustomer childernItem = List.get(childposition); SalesTools.setTextVIEwText(text,childernItem.sales_consultant_name); return convertVIEw; } @OverrIDe public Object getGroup(int groupposition) { return parentList.get(groupposition); } @OverrIDe public int getGroupCount() { return parentList.size(); } @OverrIDe public long getGroupID(int groupposition) { return groupposition; } @OverrIDe public VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEwGroup parent) { convertVIEw = inflater.inflate(R.layout.group,null); ImageVIEw iv = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.groupIcon); TextVIEw tv = (TextVIEw) convertVIEw.findVIEwByID(R.ID.groupto); if (isExpanded) { iv.setimageResource(R.drawable.btn_arrow_up); } else { iv.setimageResource(R.drawable.btn_arrow_down); } SalesTools.setTextVIEwText(tv,parentList.get(groupposition)); return convertVIEw; } @OverrIDe public boolean hasStableIDs() { return true; } @OverrIDe public boolean isChildSelectable(int groupposition,int childposition) { return true; } @OverrIDe public int getheaderState(int groupposition,int childposition) { final int childCount = getChildrenCount(groupposition); if (childposition == childCount - 1) { return PINNED_header_PUSHED_UP; } else if (childposition == -1 && !ListVIEw.isGroupExpanded(groupposition)) { return PINNED_header_GONE; } else { return PINNED_header_VISIBLE; } } @OverrIDe public voID configureheader(VIEw header,int groupposition,int Alpha) { String groupData = this.parentList.get(groupposition); ((TextVIEw) header.findVIEwByID(R.ID.groupto)).setText(groupData); } private SparseIntArray groupStatusMap = new SparseIntArray(); @OverrIDe public voID setGroupClickStatus(int groupposition,int status) { groupStatusMap.put(groupposition,status); } @OverrIDe public int getGroupClickStatus(int groupposition) { if (groupStatusMap.keyAt(groupposition) >= 0) { return groupStatusMap.get(groupposition); } else { return 0; } } public voID notifyDataSetChanged() {// 通知数据发生变化 super.notifyDataSetChanged(); } }

子条目点击事件处理:

// 子条目的点击事件 @OverrIDe public boolean onChildClick(ExpandableListVIEw parent,VIEw v,long ID) { // Toast.makeText(SalenameActivity.this,"点击了" + groupposition + // childposition,Toast.LENGTH_LONG).show(); // 将被点击的一丶二级标签的位置记录下来 String key = groupData.get(groupposition); List<DtGzsCustomer> List = map.get(key); DtGzsCustomer dtGzsCustomer = List.get(childposition); sales_consultant_name = dtGzsCustomer.sales_consultant_name; sales_consultant_ID = dtGzsCustomer.sales_consultant_ID; group_groupID = groupposition; child_childID = childposition; // 刷新界面 adapter.notifyDataSetChanged(); return true; }

ListvIEw和edittext实现批量输入:

class SetAdapter extends BaseAdapter { private List<DtGzsCustomer> goalList; private Context context; public SetAdapter(Context context,List<DtGzsCustomer> goalList) { this.context = context; this.goalList = new ArrayList<DtGzsCustomer>(); if (goalList != null) { this.goalList.addAll(goalList); } } public voID nodfiyData(List<DtGzsCustomer> goalList) { if (goalList != null) { this.goalList.clear(); this.goalList.addAll(goalList); } notifyDataSetChanged(); } @OverrIDe public int getCount() { return goalList.size(); } @OverrIDe public Object getItem(int position) { return goalList.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(final int position,VIEwGroup parent) { VIEwHolder holder; if (convertVIEw == null) { holder = new VIEwHolder(); convertVIEw = LayoutInflater.from(context).inflate(R.layout.serise_data_vIEw,false); holder.et_order = (EditText) convertVIEw.findVIEwByID(R.ID.et_order); holder.et_car = (EditText) convertVIEw.findVIEwByID(R.ID.et_car); holder.sale_name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.sale_name); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } if (position % 2 == 0) { convertVIEw.setBackgroundcolor(color.parsecolor("#e4ebf1")); } else { convertVIEw.setBackgroundcolor(color.parsecolor("#ced7de")); } final DtGzsCustomer dtGzsCustomer = goalList.get(position); removeTextWatcher(holder.et_order); removeTextWatcher(holder.et_car); String orderNum = dtGzsCustomer.order_num; holder.et_order.setText(""+orderNum); String returnNum = dtGzsCustomer.return_num; holder.et_car.setText(""+returnNum); holder.sale_name.setText(""+dtGzsCustomer.sales_consultant_name); TextWatcher orderTitle = new SimpleTextWatcher() { @OverrIDe public voID afterTextChanged(Editable s) {  int sum=0;  if (TextUtils.isEmpty(s)) {  dtGzsCustomer.order_num="";  } else {  dtGzsCustomer.order_num=String.valueOf(s).replace("0","");  }  String sales_consultant_ID = dtGzsCustomer.sales_consultant_ID;  if (!orderMap.containsKey(sales_consultant_ID)) {  String order_num = dtGzsCustomer.order_num.replace("0","");  orderMap.put(sales_consultant_ID,order_num);  }else{  orderMap.remove(sales_consultant_ID);    String order_num = dtGzsCustomer.order_num.replace("0",order_num);  }  Iterator<Map.Entry<String,String>> it = orderMap.entrySet().iterator();  while(it.hasNext()){  Map.Entry<String,String> entry = it.next();  String value = entry.getValue();  if (value==null||value.length()==0) {  value="0";  }  sum=sum+Integer.parseInt(value);  }  tv_order.setText("订车"+sum+"台"); } }; holder.et_order.addTextChangedListener(orderTitle); holder.et_order.setTag(orderTitle); TextWatcher carContent = new SimpleTextWatcher() { @OverrIDe public voID afterTextChanged(Editable s) {  int sum=0;  if (TextUtils.isEmpty(s)) {  dtGzsCustomer.return_num="";  } else {  dtGzsCustomer.return_num=String.valueOf(s).replace("0","");  }  String sales_consultant_ID = dtGzsCustomer.sales_consultant_ID;  if (!carMap.containsKey(sales_consultant_ID)) {  String return_num = dtGzsCustomer.return_num.replace("0","");  carMap.put(sales_consultant_ID,return_num);  }else{  carMap.remove(sales_consultant_ID);    String return_num = dtGzsCustomer.return_num.replace("0",return_num);  }  Iterator<Map.Entry<String,String>> it = carMap.entrySet().iterator();  while(it.hasNext()){  Map.Entry<String,String> entry = it.next();  String value = entry.getValue();  if (value==null||value.length()==0) {  value="0";  }  sum=sum+Integer.parseInt(value);  }  tv_return.setText("交车"+sum+"台"); } }; holder.et_car.addTextChangedListener(carContent); holder.et_car.setTag(carContent); return convertVIEw; } /** * 去除textWatcher * * @param editText */ private voID removeTextWatcher(EditText editText) { if (editText.getTag() instanceof TextWatcher) { editText.removeTextChangedListener((TextWatcher) editText.getTag()); } } } class VIEwHolder { EditText et_order,et_car; TextVIEw sale_name; }

以下是效果显示:

 

源码地址:http://xiazai.jb51.net/201702/yuanma/ListDemo

以上所述是小编给大家介绍的AndroID ListvIEw ExpandableListVIEw实现多选,单选,全选,edittext实现批量输入的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码全部内容,希望文章能够帮你解决Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存