Android制作簡(jiǎn)單的普通購(gòu)物車
本文實(shí)例為大家分享了Android普通購(gòu)物車制作過程,供大家參考,具體內(nèi)容如下
1.最新項(xiàng)目新增了類似購(gòu)物車功能,如下圖所示:

當(dāng)時(shí)剛看到此頁面的時(shí)候,第一反應(yīng)是利用 ListView嵌套Listview,經(jīng)過一番操作最終也實(shí)現(xiàn)了此功能。當(dāng)時(shí)也沒有考慮性能問題,只考慮能寫出來。后來嵌套數(shù)據(jù),當(dāng)數(shù)據(jù)量較大時(shí),滑動(dòng)Listview可以明顯感覺到卡頓,這對(duì)用戶來說是很難忍受的,所以才有了找到替代方案的想法,看到網(wǎng)上主流的是用ExpandableListView來實(shí)現(xiàn)此功能,所以我也用此方案來寫一下。
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, 就是說底層數(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 展開
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è)置單擊子選項(xiàng)、單擊分組項(xiàng)、分組合并、分組展開的監(jiān)聽器。
// 設(shè)置分組項(xiàng)的點(diǎn)擊監(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è)置子選項(xiàng)點(diǎn)擊監(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;
}
});
以上就是本文的全部?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)底部彈出的對(duì)話框功能
這篇文章主要介紹了Android實(shí)現(xiàn)底部彈出的對(duì)話框功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Android多種方式實(shí)現(xiàn)相機(jī)圓形預(yù)覽的示例代碼
這篇文章主要介紹了Android多種方式實(shí)現(xiàn)相機(jī)圓形預(yù)覽的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android RecyclerView上拉加載更多功能回彈實(shí)現(xiàn)代碼
這篇文章主要介紹了Android RecyclerView上拉加載更多功能回彈實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
android控件實(shí)現(xiàn)多張圖片漸變切換
這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)多張圖片漸變切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android?WebRTC?對(duì)?AudioRecord?的使用技術(shù)分享
這篇文章主要介紹了Android?WebRTC?對(duì)?AudioRecord?的使用技術(shù)分享,AudioRecord?是?Android?基于原始PCM音頻數(shù)據(jù)錄制的類,接下來和小編進(jìn)入文章了解更詳細(xì)的內(nèi)容吧2022-02-02
Android Studio gradle配置packagingOptions打包so庫重復(fù)
這篇文章主要為大家介紹了Android Studio gradle配置packagingOptions打包so庫重復(fù)問題的解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

