您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用ExpandableListView怎么實現一個二級列表購物車,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
導入依賴
compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.okhttp3:okhttp:3.9.0'
記得要把okhttp的原生文件夾復制進去,話不多說請看代碼:
MainActivity布局:
<ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全選" /> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/cb" android:text="合計:" /> <TextView android:id="@+id/tvCount" android:layout_width="100dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="#ff0000" android:gravity="center" android:text="去結算(0)" android:textColor="#ffffff" /> </RelativeLayout>
適配器的Group布局:
<CheckBox android:id="@+id/cb_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_group" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" android:text="111"/>
適配器child布局:
<CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_subhead" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvSubhead" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_marginLeft="10dp" android:id="@+id/ivDel" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_del" /> <TextView android:id="@+id/tvNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:text="1"/> <ImageView android:id="@+id/ivAdd" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_add" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/btDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除" />
接下來看代碼:
用MVP獲取的數據首先先在項目里創建三個文件夾分別是:model、view、persenter
model頁面代碼傳的參數:
public interface GoWuCheModel { public void get(String uid, Callback callback); }
model頁面的代碼:
public class MyGoWuCheModel implements GoWuCheModel { @Override public void get(String uid, Callback callback) { HashMap<String, String> map = new HashMap<>(); map.put("uid", uid); OkHttp3Utils.doPost("http://120.27.23.105/product/getCarts?source=android", map, callback); } }
view頁面的代碼:
public interface GoWuCheView { public void success(ShopBean bean); public void failuer(Exception e); }
persenter頁面的代碼:
public class GoWuChePersenter { GoWuCheView view; private final MyGoWuCheModel model; public GoWuChePersenter(GoWuCheView view) { this.view = view; model = new MyGoWuCheModel(); } public void getData(String uid) { model.get(uid, new OnUiCallback() { @Override public void onFailed(Call call, IOException e) { if (view != null) { view.failuer(e); } } @Override public void onSuccess(String result) { Gson gson = new Gson(); ShopBean bean = gson.fromJson(result, ShopBean.class); view.success(bean); } }); } public void saaa() { this.view = null; } }
MainActivity頁面代碼:
public class MainActivity extends AppCompatActivity implements GoWuCheView { private GoWuChePersenter persenter; private ExpandableListView mElv; /** * 全選 */ private CheckBox mCb; /** * 合計: */ private TextView mTvTotal; /** * 去結算(0) */ private TextView mTvCount; private ElvAdapter elvAdapter; List<ShopBean.Data> group = new ArrayList<>(); List<List<ShopBean.Data.List>> child = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); persenter = new GoWuChePersenter(this); persenter.getData("99"); mCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { elvAdapter.AllOrNone(mCb.isChecked()); } }); } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCb = (CheckBox) findViewById(R.id.cb); mTvTotal = (TextView) findViewById(R.id.tvTotal); mTvCount = (TextView) findViewById(R.id.tvCount); } public void setPriceAndCount(PriceAndCount priceAndCount) { mTvTotal.setText("合計:" + priceAndCount.getPrice()); mTvCount.setText("去結算(" + priceAndCount.getCount() + ")"); } public void setAllChecked(boolean bool) { mCb.setChecked(bool); } @Override public void success(ShopBean bean) { for (int i = 0; i < bean.getData().size(); i++) { group.add(bean.getData().get(i)); } for (int i = 0; i < group.size(); i++) { child.add(bean.getData().get(i).getList()); } elvAdapter = new ElvAdapter(this, group, child); mElv.setGroupIndicator(null); mElv.setAdapter(elvAdapter); for (int i = 0; i < group.size(); i++) { mElv.expandGroup(i); } } @Override public void failuer(Exception e) { } @Override protected void onDestroy() { super.onDestroy(); persenter.saaa(); } }
adapter頁面代碼:
public class ElvAdapter extends BaseExpandableListAdapter { private final LayoutInflater inflater; private Context context; private List<ShopBean.Data> group; private List<List<ShopBean.Data.List>> child; public ElvAdapter(Context context, List<ShopBean.Data> group, List<List<ShopBean.Data.List>> child) { this.context = context; this.group = group; this.child = child; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return group.size(); } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public Object 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() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; Log.i("============", "getGroupView: "); final GroupViewHolder holder; if (convertView == null) { view = inflater.inflate(R.layout.elv_group, null); holder = new GroupViewHolder(); holder.tv = view.findViewById(R.id.tv_group); holder.cbGroup = view.findViewById(R.id.cb_group); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } final ShopBean.Data data = group.get(groupPosition); holder.tv.setText(data.getSellerName()); holder.cbGroup.setChecked(data.isCheck()); holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改變三個CheckBox的狀態值 //1.一級列表的CheckBox的狀態值 data.setCheck(holder.cbGroup.isChecked()); //二級列表的CheckBox的狀態值 setChildrenCb(groupPosition, holder.cbGroup.isChecked()); //全選的CheckBox狀態值 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //計算錢和數量并顯示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); return view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view1; final ChildViewHolder holder1; if (convertView == null) { view1 = inflater.inflate(R.layout.elv_child, null); holder1 = new ChildViewHolder(); holder1.iv = view1.findViewById(R.id.iv); holder1.tvTitle = view1.findViewById(R.id.tv_title); holder1.tvSubhead = view1.findViewById(R.id.tvSubhead); holder1.tvPrice = view1.findViewById(R.id.tvPrice); holder1.cbChild = view1.findViewById(R.id.cb_child); holder1.btDel = view1.findViewById(R.id.btDel); holder1.tvNum = view1.findViewById(R.id.tvNum); holder1.ivDel = view1.findViewById(R.id.ivDel); holder1.ivAdd = view1.findViewById(R.id.ivAdd); view1.setTag(holder1); } else { view1 = convertView; holder1 = (ChildViewHolder) view1.getTag(); } final ShopBean.Data.List listBean = child.get(groupPosition).get(childPosition); String images = listBean.getImages(); Glide.with(context).load(images.split("\\|")[0]).into(holder1.iv); holder1.tvTitle.setText(listBean.getTitle()); holder1.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked()); holder1.tvSubhead.setText(listBean.getSubhead()); holder1.tvPrice.setText(listBean.getPrice() + "元"); holder1.tvNum.setText(listBean.getCount() + ""); holder1.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改變三個CheckBox的狀態值 //2.二級列表的checkbox狀態值 listBean.setChecked(holder1.cbChild.isChecked()); //1.一級列表的CheckBox的狀態值 group.get(groupPosition).setCheck(isAllChildCbChecked(groupPosition)); //3.全選的CheckBox狀態值 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //計算錢和數量并顯示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); //加一件商品 holder1.ivAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int count = listBean.getCount(); count++; //改變JavaBean里的狀態值 listBean.setCount(count); //計算錢和數量并顯示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); //減少一件商品 holder1.ivDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int count = listBean.getCount(); if (count <= 1) { count = 1; } else { count--; } //改變JavaBean里的狀態值 listBean.setCount(count); //計算錢和數量并顯示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); //刪除 holder1.btDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //其時就是刪除集合 List<ShopBean.Data.List> listBeans = child.get(groupPosition); if (listBeans.size() > 0) { listBeans.remove(childPosition); } if (listBeans.size() == 0) { child.remove(groupPosition); group.remove(groupPosition); } //計算錢和數量并顯示 setPriceAndCount(); //改變全選狀態 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //刷新列表 notifyDataSetChanged(); } }); return view1; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } /** * 設置一級列表對應的二級列表的CheckBox狀態 */ private void setChildrenCb(int groupPosition, boolean bool) { List<ShopBean.Data.List> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { listBeans.get(i).setChecked(bool); } } /** * 判斷一級列表CheckBox狀態 */ private boolean isAllGroupCbChecked() { if (group.size() == 0) { return false; } for (int i = 0; i < group.size(); i++) { if (!group.get(i).isCheck()) { return false; } } return true; } /** * 判斷二級列表CheckBox的狀態 */ private boolean isAllChildCbChecked(int groupPosition) { List<ShopBean.Data.List> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { if (!listBeans.get(i).isChecked()) { return false; } } return true; } /** * 設置錢和數量 */ private void setPriceAndCount(){ ((MainActivity)context).setPriceAndCount(compute()); } /** * 計算錢和數量 */ private PriceAndCount compute(){ double price = 0; int count = 0; for (int i = 0; i < group.size(); i++) { List<ShopBean.Data.List> listBeans = child.get(i); for (int j = 0; j <listBeans.size() ; j++) { if(listBeans.get(j).isChecked()){ price+=listBeans.get(j).getPrice()*listBeans.get(j).getCount(); count+=listBeans.get(j).getCount(); } } } return new PriceAndCount(price,count); } /** * 全選或者全不選 */ public void AllOrNone(boolean bool){ for (int i = 0; i < group.size(); i++) { group.get(i).setCheck(bool); setChildrenCb(i,bool); } setPriceAndCount(); notifyDataSetChanged(); } class GroupViewHolder { TextView tv; CheckBox cbGroup; } class ChildViewHolder { ImageView iv; TextView tvTitle; TextView tvSubhead; TextView tvPrice; CheckBox cbChild; TextView btDel; TextView tvNum; ImageView ivDel; ImageView ivAdd; } }
bean類代碼:
創建一個PriceAndCount 代碼
public class PriceAndCount { private double price; private int count; public PriceAndCount(double price, int count) { this.price = price; this.count = count; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
bean類:
public class ShopBean { /** * msg : 請求成功 * code : 0 * data : [{"list":[{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":1,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G顯存】全高清窄邊框 8G內存 256固態硬盤 支持指紋識別 預裝WIN10系統","title":"小米(MI)Air 13.3英寸全金屬輕薄筆記本(i5-7200U 8G 256G PCle SSD MX150 2G獨顯 FHD 指紋識別 Win10)銀\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":399,"createtime":"2017-10-02T15:20:02","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":2,"pid":81,"price":699,"pscid":85,"selected":0,"sellerid":2,"subhead":"滿2件,總價打6.50折","title":"Gap男裝 休閑舒適簡約水洗五袋直筒長褲緊身牛仔褲941825 深灰色 33/32(175/84A)"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":4,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升級4G大顯存!Nvme協議Pcie SSD,速度快人一步】GTX1050Ti就選拯救者!專業游戲鍵盤&新模具全新設計!","title":"聯想(Lenovo)拯救者R720 15.6英寸游戲筆記本電腦(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":60,"price":13888,"pscid":40,"selected":0,"sellerid":4,"subhead":"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)","title":"全球購 新款Apple MacBook Pro 蘋果筆記本電腦 銀色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"subhead":"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下","title":"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g"}],"sellerName":"商家19","sellerid":"19"}] */ private String msg; private String code; /** * list : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":1,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G顯存】全高清窄邊框 8G內存 256固態硬盤 支持指紋識別 預裝WIN10系統","title":"小米(MI)Air 13.3英寸全金屬輕薄筆記本(i5-7200U 8G 256G PCle SSD MX150 2G獨顯 FHD 指紋識別 Win10)銀\r\n"}] * sellerName : 商家1 * sellerid : 1 */ private List<Data> data; public void setMsg(String msg) { this.msg = msg; } public void setCode(String code) { this.code = code; } public void setData(List<Data> data) { this.data = data; } public String getMsg() { return msg; } public String getCode() { return code; } public List<Data> getData() { return data; } public static class Data { public boolean isCheck() { return isCheck; } public void setCheck(boolean check) { isCheck = check; } private boolean isCheck; private String sellerName; private String sellerid; /** * bargainPrice : 11800.0 * createtime : 2017-10-10T17:33:37 * detailUrl : * images : /595dd5a5Nc3a7dab5.jpg!q70.jpg * num : 1 * pid : 57 * price : 5199.0 * pscid : 40 * selected : 0 * sellerid : 1 * subhead : 【i5 MX150 2G顯存】全高清窄邊框 8G內存 256固態硬盤 支持指紋識別 預裝WIN10系統 * title : 小米(MI)Air 13.3英寸全金屬輕薄筆記本(i5-7200U 8G 256G PCle SSD MX150 2G獨顯 FHD 指紋識別 Win10)銀 */ private java.util.List<List> list; public void setSellerName(String sellerName) { this.sellerName = sellerName; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public void setList(java.util.List<List> list) { this.list = list; } public String getSellerName() { return sellerName; } public String getSellerid() { return sellerid; } public java.util.List<List> getList() { return list; } public static class List { private boolean isChecked; public boolean isChecked() { return isChecked; } public void setChecked(boolean checked) { isChecked = checked; } private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public void setCreatetime(String createtime) { this.createtime = createtime; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public void setImages(String images) { this.images = images; } public void setNum(int num) { this.num = num; } public void setPid(int pid) { this.pid = pid; } public void setPrice(double price) { this.price = price; } public void setPscid(int pscid) { this.pscid = pscid; } public void setSelected(int selected) { this.selected = selected; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public void setSubhead(String subhead) { this.subhead = subhead; } public void setTitle(String title) { this.title = title; } public double getBargainPrice() { return bargainPrice; } public String getCreatetime() { return createtime; } public String getDetailUrl() { return detailUrl; } public String getImages() { return images; } public int getNum() { return num; } public int getPid() { return pid; } public double getPrice() { return price; } public int getPscid() { return pscid; } public int getSelected() { return selected; } public int getSellerid() { return sellerid; } public String getSubhead() { return subhead; } public String getTitle() { return title; } } } }
關于使用ExpandableListView怎么實現一個二級列表購物車就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。