Android listview ExpandableListView實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼
最近在項(xiàng)目開(kāi)發(fā)中,由于項(xiàng)目的需求要實(shí)現(xiàn)一些列表的單選,多選,全選,批量輸入之類的功能,其實(shí)功能的實(shí)現(xiàn)倒不是很復(fù)雜,需求中也沒(méi)有涉及到復(fù)雜的動(dòng)畫(huà)什么之類,主要是解決列表數(shù)據(jù)復(fù)用的問(wèn)題,解決好這個(gè)就可以了。下面是最近項(xiàng)目中涉及到的一些:
listview實(shí)現(xiàn)多選、全選、取消全選:
下面是適配器,一開(kāi)始在適配器的構(gòu)造函數(shù)中,對(duì)數(shù)據(jù)進(jìn)行初始化,同時(shí)定義一個(gè)集合用于管理listview的點(diǎn)擊;
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的數(shù)據(jù) 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); } // 初始化的時(shí)候做處理,如果管理標(biāo)記的集合中沒(méi)有該pos則設(shè)為false如果有則設(shè)為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); // 數(shù)量設(shè)為list的長(zhǎng)度 checkNum = list.size(); // 刷新listview和TextView的顯示 cb_all.setTextColor(Color.parseColor("#0097e0")); dataChanged(); }
這里是條目點(diǎn)擊事件:
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { DtGzsCustomer dtGzsCustomer = list.get(position); // 取得ViewHolder對(duì)象,這樣就省去了通過(guò)層層的findViewById去實(shí)例化我們需要的cb實(shí)例的步驟 ViewHolder holder = (ViewHolder) view.getTag(); // 改變CheckBox的狀態(tài) holder.cb.toggle(); // 將CheckBox的選中狀況記錄下來(lái) adapter.getIsSelected().put(position, holder.cb.isChecked()); // 調(diào)整選定條目 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實(shí)現(xiàn)單個(gè)條目單選,整個(gè)列表多線:
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, View convertView, 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實(shí)現(xiàn)子條目單選:
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);// 根據(jù)組名位置的值作為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, View convertView, 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的位置是否相同,如相同,則表示為選中狀態(tài),更改其背景顏色,如不相同,則設(shè)置背景色為白色 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, View convertView, 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 childPosition, 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() {// 通知數(shù)據(jù)發(fā)生變化 super.notifyDataSetChanged(); } }
子條目點(diǎn)擊事件處理:
// 子條目的點(diǎn)擊事件 @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // Toast.makeText(SaleNameActivity.this, "點(diǎn)擊了" + groupPosition + // childPosition, Toast.LENGTH_LONG).show(); // 將被點(diǎn)擊的一丶二級(jí)標(biāo)簽的位置記錄下來(lái) 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實(shí)現(xiàn)批量輸入:
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, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.serise_data_view, parent, 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", ""); orderMap.put(sales_consultant_id, 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+"臺(tái)"); } }; 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", ""); carMap.put(sales_consultant_id, 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+"臺(tái)"); } }; 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實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
- Android ScrollView嵌套ExpandableListView顯示不正常的問(wèn)題的解決辦法
- Android 關(guān)于ExpandableListView刷新問(wèn)題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView展開(kāi)列表控件使用實(shí)例
- Android ExpandableListView用法示例詳解
相關(guān)文章
Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android WebView上實(shí)現(xiàn)JavaScript與Java交互
這篇文章主要介紹了Android WebView上實(shí)現(xiàn)JavaScript與Java交互 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼
EditText搜索結(jié)果下拉框、自動(dòng)or回調(diào)模式、可diy、使用超簡(jiǎn)便。這篇文章主要介紹了Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼,需要的朋友可以參考下2019-10-10Android 操作系統(tǒng)獲取Root權(quán)限 原理詳細(xì)解析
許多機(jī)友新購(gòu)來(lái)的Android機(jī)器沒(méi)有破解過(guò)Root權(quán)限,無(wú)法使用一些需要高權(quán)限的軟件,以及進(jìn)行一些高權(quán)限的操作,其實(shí)破解手機(jī)Root權(quán)限是比較簡(jiǎn)單及安全的,破解Root權(quán)限的原理就是在手機(jī)的/system/bin/或/system/xbin/目錄下放置一個(gè)可執(zhí)行文件“su”2013-10-10UIImage初始化的區(qū)別兩種方法介紹(面試常見(jiàn))
本文通過(guò)兩種方法給大家介紹UIImage初始化的區(qū)別,在面試過(guò)程中經(jīng)常遇到,對(duì)uiimage初始化相關(guān)知識(shí)感興趣的朋友一起了解下吧2016-05-05Android開(kāi)發(fā)中使用WebView控件瀏覽網(wǎng)頁(yè)的方法詳解
這篇文章主要介紹了Android開(kāi)發(fā)中使用WebView控件瀏覽網(wǎng)頁(yè)的方法,結(jié)合實(shí)例形式較為詳細(xì)的總結(jié)分析了Android WebView控件的功能、布局、設(shè)置、常用方法及相關(guān)操作技巧,需要的朋友可以參考下2017-10-10