Android制作簡單的普通購物車
本文實例為大家分享了Android普通購物車制作過程,供大家參考,具體內(nèi)容如下
1.最新項目新增了類似購物車功能,如下圖所示:
當(dāng)時剛看到此頁面的時候,第一反應(yīng)是利用 ListView嵌套Listview,經(jīng)過一番操作最終也實現(xiàn)了此功能。當(dāng)時也沒有考慮性能問題,只考慮能寫出來。后來嵌套數(shù)據(jù),當(dāng)數(shù)據(jù)量較大時,滑動Listview可以明顯感覺到卡頓,這對用戶來說是很難忍受的,所以才有了找到替代方案的想法,看到網(wǎng)上主流的是用ExpandableListView來實現(xiàn)此功能,所以我也用此方案來寫一下。
2.成型后的Demo,如下圖所示:
3.思路:
1).選用ExpandableListView作為控件
2).給每個數(shù)據(jù)源添加一個選中標(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) { // 初始化組、子列表項 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; // 初始化組、子列表項 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() { //分組和子選項是否持有穩(wěn)定的ID, 就是說底層數(shù)據(jù)的改變會不會影響到它們 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 展開
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
通過方法名我們就能知道各自的用途,它們分別設(shè)置單擊子選項、單擊分組項、分組合并、分組展開的監(jiān)聽器。
// 設(shè)置分組項的點擊監(jiān)聽事件 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è)置子選項點擊監(jiān)聽事件 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; } });
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)購物車功能
- Android實現(xiàn)的仿淘寶購物車demo示例
- Android實現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能demo示例
- Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
- Android把商品添加到購物車的動畫效果(貝塞爾曲線)
- Android實現(xiàn)簡單購物車功能
- Android仿外賣購物車功能
- Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實例詳解)
- Android實現(xiàn)購物車添加物品的動畫效果
- Android實現(xiàn)二級購物車的全選加反選、總價功能
相關(guān)文章
Android編程實現(xiàn)獲得內(nèi)存剩余大小與總大小的方法
這篇文章主要介紹了Android編程實現(xiàn)獲得內(nèi)存剩余大小與總大小的方法,涉及Android基于ActivityManager實現(xiàn)內(nèi)存信息的操作技巧,需要的朋友可以參考下2015-12-12Android-ViewModel和LiveData使用詳解
這篇文章主要介紹了Android-ViewModel和LiveData使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用
本文主要介紹了Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)
本篇文章主要介紹了android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer),具有一定的參考價值,有興趣的可以了解一下2017-08-08Android手冊之Toolbar搜索聯(lián)動及監(jiān)聽小技巧
這篇文章主要為大家介紹了Android手冊之Toolbar搜索聯(lián)動及監(jiān)聽小技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android Filterable實現(xiàn)Recyclerview篩選功能的示例代碼
這篇文章主要介紹了Android Filterable實現(xiàn)Recyclerview篩選功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02android中AutoCompleteTextView的簡單用法(實現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡單用法(自動提示),有需要的可以了解一下。2016-11-11