Android实现二级列表购物车功能
程序员文章站
2023-11-26 16:39:28
本文实例为大家分享了android实现二级列表购物车功能的具体代码,供大家参考,具体内容如下
mainactivity:
package com.baway...
本文实例为大家分享了android实现二级列表购物车功能的具体代码,供大家参考,具体内容如下
mainactivity:
package com.baway.twoshopcar; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.checkbox; import android.widget.expandablelistview; import android.widget.textview; import org.greenrobot.eventbus.eventbus; import org.greenrobot.eventbus.subscribe; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity implements view.onclicklistener { /** * 全选 */ private checkbox mcball; /** * 22 */ private textview mtotalprice; /** * 22 */ private textview mtotalnum; private expandablelistview melv; private list<groupbean> grouplist = new arraylist<>(); private list<list<childbean>> childlist = new arraylist<>(); private myadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); eventbus.getdefault().register(this); initview(); initdate(); melv.setgroupindicator(null); adapter = new myadapter(this, grouplist, childlist); melv.setadapter(adapter); for (int i = 0; i <grouplist.size() ; i++) { melv.expandgroup(i); } } @subscribe public void moneycount(mcevent mcevent){ int money = mcevent.getmoney(); int count = mcevent.getcount(); mtotalnum.settext(count+""); mtotalprice.settext(money+""); } @subscribe public void messageevent(msgevent msg) { mcball.setchecked(msg.isflag()); } @override protected void ondestroy() { super.ondestroy(); eventbus.getdefault().unregister(this); } private void initdate() { for (int i = 0; i < 3; i++) { groupbean groupbean = new groupbean(false, "商家" + i); grouplist.add(groupbean); list<childbean> list = new arraylist<>(); for (int j = 0; j < 2; j++) { childbean childbean = new childbean("商品" + i, 1 + i, false); list.add(childbean); } childlist.add(list); } } private void initview() { mcball = (checkbox) findviewbyid(r.id.cb_all); mcball.setonclicklistener(this); mtotalprice = (textview) findviewbyid(r.id.totalprice); mtotalnum = (textview) findviewbyid(r.id.totalnum); melv = (expandablelistview) findviewbyid(r.id.elv); } @override public void onclick(view v) { switch (v.getid()) { case r.id.cb_all: adapter.allchecked(mcball.ischecked()); break; } } }
myadapter适配器:
package com.baway.twoshopcar; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.checkbox; import android.widget.textview; import org.greenrobot.eventbus.eventbus; import java.util.list; /** * created by 郑文杰 on 2017/10/24. */ public class myadapter extends baseexpandablelistadapter { private context context; private list<groupbean> grouplist; private list<list<childbean>> childlist; private int count; private int summoney; public myadapter(context context, list<groupbean> grouplist, list<list<childbean>> childlist) { this.context = context; this.grouplist = grouplist; this.childlist = childlist; } @override public int getgroupcount() { return grouplist.size(); } @override public int getchildrencount(int groupposition) { return childlist.get(groupposition).size(); } @override public object getgroup(int groupposition) { return grouplist.get(groupposition); } @override public object getchild(int groupposition, int childposition) { return childlist.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(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { groupviewholder holder; if (convertview == null) { holder = new groupviewholder(); convertview = view.inflate(context, r.layout.groupitem, null); holder.cb = convertview.findviewbyid(r.id.cb); holder.tvname = convertview.findviewbyid(r.id.tvname); convertview.settag(holder); } else { holder = (groupviewholder) convertview.gettag(); } //赋值 groupbean groupbean = grouplist.get(groupposition); holder.cb.setchecked(groupbean.ischecked()); holder.tvname.settext(groupbean.getgroupname()); //给group设置点击事件 holder.cb.setonclicklistener(new groupcbonclicklistener(groupposition)); return convertview; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { childviewholder holder; if (convertview == null) { holder = new childviewholder(); convertview = view.inflate(context, r.layout.childitem, null); holder.cb = convertview.findviewbyid(r.id.cb); holder.tvname = convertview.findviewbyid(r.id.tvname); holder.tvprice = convertview.findviewbyid(r.id.tvprice); convertview.settag(holder); } else { holder = (childviewholder) convertview.gettag(); } //赋值 childbean childbean = childlist.get(groupposition).get(childposition); holder.cb.setchecked(childbean.ischecked()); holder.tvname.settext(childbean.getchildname()); holder.tvprice.settext(childbean.getprice() + ""); //设置点击事件child holder.cb.setonclicklistener(new childcbonclicklistener(groupposition,childposition)); return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } class groupviewholder { checkbox cb; textview tvname; } class childviewholder { checkbox cb; textview tvname; textview tvprice; } class childcbonclicklistener implements view.onclicklistener { private int groupposition; private int childposition; public childcbonclicklistener(int groupposition, int childposition) { this.groupposition = groupposition; this.childposition = childposition; } @override public void onclick(view v) { if (v instanceof checkbox) { checkbox cb = (checkbox) v; list<childbean> childbeen = childlist.get(groupposition); childbean childbean = childbeen.get(childposition); childbean.setchecked(cb.ischecked()); //计算选中的商品数,并发送到主界面进行显示 mcevent mcevent = new mcevent(); mcevent.setcount(totalcount()); mcevent.setmoney(totalprice()); eventbus.getdefault().post(mcevent); //判断商家所有的商品的checkbox是否选中 if (ischildchecked(childbeen)) { grouplist.get(groupposition).setchecked(true); msgevent msgevent = new msgevent(); msgevent.setflag(isgroupchecked()); eventbus.getdefault().post(msgevent); notifydatasetchanged(); } else { grouplist.get(groupposition).setchecked(false); msgevent msgevent = new msgevent(); msgevent.setflag(false); msgevent.setflag(isgroupchecked()); eventbus.getdefault().post(msgevent); notifydatasetchanged(); } } } } /** * 判断所有商家的所有商品的checkbox是否都选中 * * @param childbean * @return */ private boolean ischildchecked(list<childbean> childbean) { for (int i = 0; i < childbean.size(); i++) { childbean childbean1 = childbean.get(i); if (!childbean1.ischecked()) { return false; } } return true; } class groupcbonclicklistener implements view.onclicklistener { private int groupposition; public groupcbonclicklistener(int groupposition) { this.groupposition = groupposition; } @override public void onclick(view v) { if (v instanceof checkbox){ checkbox cb= (checkbox) v; //根据cb.ischecked()是否选中,给一级列的checkbox改变状态 grouplist.get(groupposition).setchecked(cb.ischecked()); list<childbean> childbeenlist = childlist.get(groupposition); for (childbean childbean : childbeenlist){ childbean.setchecked(cb.ischecked()); } //计算选中的商品数和金额,并发送到主界面进行显示 mcevent mcevent = new mcevent(); mcevent.setcount(totalcount()); mcevent.setmoney(totalprice()); eventbus.getdefault().post(mcevent); msgevent msgevent = new msgevent(); msgevent.setflag(isgroupchecked()); eventbus.getdefault().post(msgevent); notifydatasetchanged(); } } } /** * 判断其他商家是否选中 * @return */ private boolean isgroupchecked() { for (groupbean groupbean : grouplist) { if (!groupbean.ischecked()){ return false; } } return true; } //主界面全选框选中状态 public void allchecked(boolean bool) { for (int i = 0; i < grouplist.size(); i++) { grouplist.get(i).setchecked(bool); for (int j = 0; j < childlist.get(i).size(); j++) { childlist.get(i).get(j).setchecked(bool); } } //计算选中的商品数,发送到主界面进行显示 mcevent mcevent = new mcevent(); mcevent.setcount(totalcount()); mcevent.setmoney(totalprice()); eventbus.getdefault().post(mcevent); notifydatasetchanged(); } /** * 计算商品总价格 * * @return */ private int totalprice() { summoney = 0; for (int i = 0; i < grouplist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { if (childlist.get(i).get(j).ischecked()) { int price = childlist.get(i).get(j).getprice(); summoney += price; } } } return summoney; } /** * 计算商品的总数量 * * @return */ private int totalcount() { count = 0; for (int i = 0; i < grouplist.size(); i++) { for (int j = 0; j < childlist.get(i).size(); j++) { if (childlist.get(i).get(j).ischecked()) { //遍历所有商品,只要是选中状态的,就加1 count++; } } } return count; } }
childbean:
package com.baway.twoshopcar; /** * created by hhh on 2017/10/24. */ public class childbean { private string childname; private int price; private boolean checked; public childbean(string childname, int price, boolean checked) { this.childname = childname; this.price = price; this.checked = checked; } public string getchildname() { return childname; } public void setchildname(string childname) { this.childname = childname; } public int getprice() { return price; } public void setprice(int price) { this.price = price; } public boolean ischecked() { return checked; } public void setchecked(boolean checked) { this.checked = checked; } }
groupbean:
package com.baway.twoshopcar; /** * created by hhh on 2017/10/24. */ public class groupbean { private boolean checked; private string groupname; public groupbean(boolean checked, string groupname) { this.checked = checked; this.groupname = groupname; } public boolean ischecked() { return checked; } public void setchecked(boolean checked) { this.checked = checked; } public string getgroupname() { return groupname; } public void setgroupname(string groupname) { this.groupname = groupname; } }
mcevent:
package com.baway.twoshopcar; /** * created by hhhh on 2017/10/24. */ public class mcevent { private int money; private int count; public int getmoney() { return money; } public void setmoney(int money) { this.money = money; } public int getcount() { return count; } public void setcount(int count) { this.count = count; } }
msgevent:
package com.baway.twoshopcar; /** * created by hhh on 2017/10/24. */ public class msgevent { private boolean flag; public boolean isflag() { return flag; } public void setflag(boolean flag) { this.flag = flag; } }
mainactivity布局文件:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.baway.twoshopcar.mainactivity"> <linearlayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:orientation="horizontal"> <checkbox android:id="@+id/cb_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="全选" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="20dp" android:gravity="center_vertical" android:text="合计:" /> <textview android:id="@+id/totalprice" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="10dp" android:gravity="center_vertical" android:text="22" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="30dp" android:gravity="center_vertical" android:text="数量" /> <textview android:id="@+id/totalnum" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="10dp" android:gravity="center_vertical" android:text="22" /> </linearlayout> <expandablelistview android:id="@+id/elv" android:layout_above="@id/ll" android:layout_width="match_parent" android:layout_height="match_parent"></expandablelistview> </relativelayout>
childitem布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#330000ff" android:paddingleft="20dp" android:gravity="center_vertical" android:orientation="horizontal"> <checkbox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/tvname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" /> <textview android:id="@+id/tvprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="60dp" /> </linearlayout>
groupitem布局:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#330000ff" android:gravity="center_vertical" android:orientation="horizontal"> <checkbox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/tvname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" /> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。