Android制作簡(jiǎn)單的普通購(gòu)物車
本文實(shí)例為大家分享了Android普通購(gòu)物車制作過(guò)程,供大家參考,具體內(nèi)容如下
1.最新項(xiàng)目新增了類似購(gòu)物車功能,如下圖所示:
當(dāng)時(shí)剛看到此頁(yè)面的時(shí)候,第一反應(yīng)是利用 ListView嵌套Listview,經(jīng)過(guò)一番操作最終也實(shí)現(xiàn)了此功能。當(dāng)時(shí)也沒(méi)有考慮性能問(wèn)題,只考慮能寫出來(lái)。后來(lái)嵌套數(shù)據(jù),當(dāng)數(shù)據(jù)量較大時(shí),滑動(dòng)Listview可以明顯感覺(jué)到卡頓,這對(duì)用戶來(lái)說(shuō)是很難忍受的,所以才有了找到替代方案的想法,看到網(wǎng)上主流的是用ExpandableListView來(lái)實(shí)現(xiàn)此功能,所以我也用此方案來(lái)寫一下。
2.成型后的Demo,如下圖所示:
3.思路:
1).選用ExpandableListView作為控件
2).給每個(gè)數(shù)據(jù)源添加一個(gè)選中標(biāo)志,isChecked,根據(jù)ischecked,控制checkbox的狀態(tài);
ExpandableListView相關(guān)普及
#1.首先看下ExpandableListView的adapter,與普通的ListView adapter不同
public class MyExpandAdapter extends BaseExpandableListAdapter { //紅色部分的數(shù)據(jù)源 List<OrderDetailsEntity> group_head = new ArrayList(); //內(nèi)層部分?jǐn)?shù)據(jù)源 List<List<ProductDetails>> child = new ArrayList(); Context context; LayoutInflater inflater; public MyExpandAdapter(Context content) { // 初始化組、子列表項(xiàng) group_head = new ArrayList<OrderDetailsEntity>(); child = new ArrayList<List<ProductDetails>>(); inflater = LayoutInflater.from(context); } public MyExpandAdapter(Context context, List<OrderDetailsEntity> group_head, List<List<ProductDetails>> child) { this.context = context; // 初始化組、子列表項(xiàng) this.group_head = group_head; this.child = child; inflater = LayoutInflater.from(context); } /****************************跟普通adapter一樣******************************/ @Override public int getGroupCount() { return this.group_head.size(); } @Override public int getChildrenCount(int position) { if (position < 0 || position >= this.child.size()) return 0; return child.get(position).size(); } @Override public OrderDetailsEntity getGroup(int groupPosition) { return group_head.get(groupPosition); } @Override public ProductDetails getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } /**************************************************************************/ @Override public boolean hasStableIds() { //分組和子選項(xiàng)是否持有穩(wěn)定的ID, 就是說(shuō)底層數(shù)據(jù)的改變會(huì)不會(huì)影響到它們 return true; } private boolean isSelectAll(OrderDetailsEntity entity) { int count = 0; for (ProductDetails p : entity.getProductDetails()) { if (p.isChecked()) { count++; } } return entity.getProductDetails().size() == count; } /*************************與普通adapter的getView方法一樣**********************/ @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolderGroup group; if (convertView == null) { convertView = inflater.inflate(R.layout.item, parent, false); group = new ViewHolderGroup(); group.cb_all = (CheckBox) convertView.findViewById(R.id.cb_checkAll); group.tv_name = (TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(group); } else { group = (ViewHolderGroup) convertView.getTag(); } group.tv_name.setText( group_head.get(groupPosition).getMemberId() + "," + group_head.get(groupPosition).getShopName()); group.cb_all.setChecked(isSelectAll(getGroup(groupPosition))); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolderChild childV; if (convertView == null) { convertView = inflater.inflate(R.layout.item_inner, parent, false); childV = new ViewHolderChild(); childV.cb = (CheckBox) convertView.findViewById(R.id.cb_check); childV.iv = (ImageView) convertView.findViewById(R.id.iv_img); childV.name = (TextView) convertView.findViewById(R.id.name); convertView.setTag(childV); } else { childV = (ViewHolderChild) convertView.getTag(); } childV.name.setText(getChild(groupPosition, childPosition).getProductName()); childV.cb.setChecked(getChild(groupPosition, childPosition).isChecked()); return convertView; } /****************************************************************************/ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { //// 指定位置的子視圖是否可選擇。 // 調(diào)用Activity里的ChildSelect方法 // Toast.makeText(context, "gp="+groupPosition+",cp="+childPosition, // Toast.LENGTH_LONG).show(); return true; } static class ViewHolderGroup { CheckBox cb_all; TextView tv_name; } static class ViewHolderChild { CheckBox cb; ImageView iv; TextView name; } }
#2.ExpandableListView 展開(kāi)
for (int i = 0; i < adapter.getGroupCount(); i++) { sp_date_list.expandGroup(i); }
3).ExpandableListView 去掉默認(rèn)圖標(biāo)
sp_date_list.setGroupIndicator(null);
4).ExpandableListView itemClick事件處理
1. setOnChildClickListener
2. setOnGroupClickListener
3. setOnGroupCollapseListener
4. setOnGroupExpandListener
通過(guò)方法名我們就能知道各自的用途,它們分別設(shè)置單擊子選項(xiàng)、單擊分組項(xiàng)、分組合并、分組展開(kāi)的監(jiān)聽(tīng)器。
// 設(shè)置分組項(xiàng)的點(diǎn)擊監(jiān)聽(tīng)事件 expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { Toast.makeText(getApplicationContext(), groupStrings[i], Toast.LENGTH_SHORT).show(); //如果處理事件,return true,默認(rèn)return false return false; } // 設(shè)置子選項(xiàng)點(diǎn)擊監(jiān)聽(tīng)事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), childStrings[groupPosition][childPosition], Toast.LENGTHshow(); return true; } });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)購(gòu)物車功能
- Android實(shí)現(xiàn)的仿淘寶購(gòu)物車demo示例
- Android實(shí)現(xiàn)仿淘寶購(gòu)物車增加和減少商品數(shù)量功能demo示例
- Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯
- Android把商品添加到購(gòu)物車的動(dòng)畫效果(貝塞爾曲線)
- Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
- Android仿外賣購(gòu)物車功能
- Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)
- Android實(shí)現(xiàn)購(gòu)物車添加物品的動(dòng)畫效果
- Android實(shí)現(xiàn)二級(jí)購(gòu)物車的全選加反選、總價(jià)功能
相關(guān)文章
Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法,涉及Android基于ActivityManager實(shí)現(xiàn)內(nèi)存信息的操作技巧,需要的朋友可以參考下2015-12-12Android-ViewModel和LiveData使用詳解
這篇文章主要介紹了Android-ViewModel和LiveData使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用
本文主要介紹了Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Android 三種動(dòng)畫詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04android視頻播放簡(jiǎn)單實(shí)現(xiàn)示例(VideoView&MediaPlayer)
本篇文章主要介紹了android視頻播放簡(jiǎn)單實(shí)現(xiàn)示例(VideoView&MediaPlayer),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android手冊(cè)之Toolbar搜索聯(lián)動(dòng)及監(jiān)聽(tīng)小技巧
這篇文章主要為大家介紹了Android手冊(cè)之Toolbar搜索聯(lián)動(dòng)及監(jiān)聽(tīng)小技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼
這篇文章主要介紹了Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡(jiǎn)單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11