大佬教程收集整理的这篇文章主要介绍了Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
最近在项目开发中,由于项目的需求要实现一些列表的单选,多选,全选,批量输入之类的功能,其实功能的实现倒不是很复杂,需求中也没有涉及到复杂的动画什么之类,主要是解决列表数据复用的问题,解决好这个就可以了。下面是最近项目中涉及到的一些:
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.al@R_772_5352@n = (TextView) convertView.findViewById(R.id.al@R_772_5352@n); 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.al@R_772_5352@n.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.al@R_772_5352@n,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,al@R_772_5352@n,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和ra@L_801_14@nbutton实现单个条目单选,整个列表多线:
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 = (Ra@L_801_14@Button) convertView.findViewById(R.id.scb); holder.scc = (Ra@L_801_14@Button) convertView.findViewById(R.id.scc); holder.scd = (Ra@L_801_14@Button) convertView.findViewById(R.id.scd); holder.sce = (Ra@L_801_14@Button) 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; Ra@L_801_14@Button 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.code.net/201702/yuanma/ListDemo
以上所述是小编给大家介绍的Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
以上是大佬教程为你收集整理的Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码全部内容,希望文章能够帮你解决Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。