一,依赖
compile 'com.jakewharton:butterknife:8.4.0' compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'com.facebook.fresco:fresco:0.14.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.jcodecraeer:xrecyclerview:1.3.2'
二,权限
<uses-permission android:name="android.permission.INTERNET" />
三,main 布局
xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="ywf.bwei.com.lianxi_gouwuche.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal"> <TextView android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="购物车" android:textSize="40px" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编辑" android:textSize="40px" /> LinearLayout> <ExpandableListView android:id="@+id/edlist" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" android:groupIndicator="@null">ExpandableListView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选/反选" android:textSize="30px" /> <LinearLayout android:id="@+id/bianji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30px" android:text="合计:" android:textSize="30px" /> <TextView android:id="@+id/hj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30px" android:text="0" android:textSize="30px" /> <Button android:id="@+id/jiesuan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50px" android:background="#ff0000" android:text="去结算" android:textSize="30px" /> LinearLayout> <LinearLayout android:id="@+id/wancheng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/shanchu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="150px" android:text="删除" android:textSize="30px" /> LinearLayout> LinearLayout> LinearLayout>
gouwuche_chiled 布局
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="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/child_ck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:focusable="false" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/child_sd" android:layout_width="200px" android:layout_height="200px" android:layout_marginLeft="50px" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20px" android:orientation="vertical"> <TextView android:id="@+id/cjild_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好啊我来拉别跑啊" /> <TextView android:id="@+id/cjild_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60px" android:text="你好啊我来拉别跑啊" /> LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-150dp" android:layout_marginTop="180px" android:orientation="horizontal"> <ImageView android:id="@+id/jian" android:layout_width="40px" android:layout_height="40px" android:focusable="false" android:src="@drawable/shopcart_minus_grey" /> <EditText android:id="@+id/ed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" /> <ImageView android:id="@+id/jia" android:layout_width="40px" android:layout_height="40px" android:focusable="false" android:src="@drawable/shopcart_add_red" /> LinearLayout> LinearLayout> LinearLayout>
gouwuche_prenter 布局
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="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/prenter_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" /> <TextView android:id="@+id/prenter_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20px" /> LinearLayout> LinearLayout>
//注意 需要三张图片
三,创建model,view,persenter,bean,adapter,utils 包
model 层的 Mymode 的类
public class Mymode { public void getgouwuche(final Getgouwuchebean getgouwuchebean) {
getmInstance 需要全删了,重打
Util.getmInstance().getnetjson(Getnet.net).getuser() .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer() { @Override public void accept(Gouwuche gouwuche) throws Exception { getgouwuchebean.getgouwuchejson(gouwuche); } }); } public interface Getgouwuchebean { void getgouwuchejson(Gouwuche gouwuche); } }
persenter 层的 Ipersenter 的接口
public interface Ipersenter { void onSuccess(Listdata); //是bean里面的List集合 void onFailed(String msg); void onDestory(); void getCarInfo(String uid); }
persenter 层的 Mypresenter 的类
public class Mypresenter implements Ipersenter { private Context context; private Iview iview; private Mymode mymode; public Mypresenter(Context context, Iview iview) { this.context = context; this.iview = iview; mymode = new Mymode(); } public void getmv() { mymode.getgouwuche(new Mymode.Getgouwuchebean() { @Override public void getgouwuchejson(Gouwuche gouwuche) { Listdata = gouwuche.getData(); iview.setadapter(context, data); } }); } @Override public void onSuccess(List data) { if (iview != null) { iview.setadapter(context, data); } } @Override public void onFailed(String msg) { if (iview != null) { iview.onFailed(msg); } } @Override public void onDestory() { if (iview != null) { iview = null; } } @Override public void getCarInfo(String uid) { } }
view 层的 Iview 的接口
public interface Iview { void setadapter(Context context, Listdata); void onFailed(String msg); void heji(); }
bean 包下的 Gouwuche
public class Gouwuche { /** * msg : 请求成功 * code : 0 * data : [{"list":[],"sellerName":"","sellerid":"0"},{"list":[{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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":61,"price":14999,"pscid":40,"selected":0,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":50,"price":444,"pscid":39,"selected":0,"sellerid":6,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":4,"pid":56,"price":99,"pscid":39,"selected":1,"sellerid":12,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家12","sellerid":"12"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":40,"price":345,"pscid":2,"selected":1,"sellerid":17,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家17","sellerid":"17"}] */ private String msg; private String code; private Listdata; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * list : [] * sellerName : * sellerid : 0 */ private String sellerName; private String sellerid; private boolean presenterck; public boolean isPresenterck() { return presenterck; } public void setPresenterck(boolean presenterck) { this.presenterck = presenterck; } private List list; public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List getList() { return list; } public void setList(List list) { this.list = list; } } public class Mybean{ /** * bargainPrice : 99 * createtime : 2017-10-14T21:38:26 * detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg * num : 1 * pid : 45 * price : 2999 * pscid : 39 * selected : 0 * sellerid : 1 * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽! * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机 */ private int count; private boolean childck; private double bargainPrice; private String createtime; private String detailUrl; private String images; private double num; private double pid; private double price; private double pscid; private double selected; private double sellerid; private String subhead; private String title; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public boolean isChildck() { return childck; } public void setChildck(boolean childck) { this.childck = childck; } public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public double getNum() { return num; } public void setNum(double num) { this.num = num; } public double getPid() { return pid; } public void setPid(double pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getPscid() { return pscid; } public void setPscid(double pscid) { this.pscid = pscid; } public double getSelected() { return selected; } public void setSelected(double selected) { this.selected = selected; } public double getSellerid() { return sellerid; } public void setSellerid(double sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
adapter 包 Myadapter
public class Myadapter extends BaseExpandableListAdapter { private Context context; private Listdata; private Iview iview; public Myadapter(Context context, List data, Iview iview) { this.context = context; this.data = data; this.iview = iview; } @Override public int getGroupCount() { return data.size(); } @Override public int getChildrenCount(int groupPosition) { return data.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return data.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return data.get(groupPosition).getList().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) { //这个Mypresenthodler需要在下面定义 Mypresenthodler mypresenthodler = null; if (convertView == null) { convertView = View.inflate(context, R.layout.gouwuche_prenter, null); CheckBox prenter_cb = convertView.findViewById(R.id.prenter_cb); TextView prenter_tv = convertView.findViewById(R.id.prenter_tv); mypresenthodler = new Mypresenthodler(prenter_cb, prenter_tv); convertView.setTag(mypresenthodler); } else { mypresenthodler = (Mypresenthodler) convertView.getTag(); } mypresenthodler.prenter_tv.setText(data.get(groupPosition).getSellerName()); mypresenthodler.prenter_cb.setChecked(data.get(groupPosition).isPresenterck()); //父类的点击事件 mypresenthodler.prenter_cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Gouwuche.DataBean dataBean = data.get(groupPosition); boolean isCheked = dataBean.isPresenterck(); isCheked = !isCheked; dataBean.setPresenterck(isCheked); List list = Myadapter.this.data.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { list.get(i).setChildck(isCheked); } iview.heji(); notifyDataSetChanged(); } }); return convertView; } @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //这个Mychildhodler需要在下面定义 Mychildhodler mychildhodler = null; if (convertView == null) { convertView = View.inflate(context, R.layout.gouwuche_chiled, null); CheckBox child_ck = convertView.findViewById(R.id.child_ck); SimpleDraweeView child_sd = convertView.findViewById(R.id.child_sd); TextView cjild_name = convertView.findViewById(R.id.cjild_name); TextView cjild_price = convertView.findViewById(R.id.cjild_price); ImageView jian = convertView.findViewById(R.id.jian); EditText ed = convertView.findViewById(R.id.ed); ImageView jia = convertView.findViewById(R.id.jia); mychildhodler = new Mychildhodler(child_ck, child_sd, cjild_name, cjild_price, jian, ed, jia); convertView.setTag(mychildhodler); } else { mychildhodler = (Mychildhodler) convertView.getTag(); } final Gouwuche.Mybean mybean = data.get(groupPosition).getList().get(childPosition); mychildhodler.child_ck.setChecked(mybean.isChildck()); String images = mybean.getImages(); String[] split = images.split(".jpg"); mychildhodler.child_sd.setImageURI(Uri.parse(split[0] + ".jpg")); mychildhodler.cjild_name.setText(mybean.getTitle()); mychildhodler.cjild_price.setText(mybean.getPrice() + ""); mychildhodler.ed.setText(mybean.getCount() + 1 + ""); mychildhodler.child_ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isCheked = mybean.isChildck(); isCheked = !isCheked; mybean.setChildck(isCheked); boolean flag = true; List list = Myadapter.this.data.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { if (!list.get(i).isChildck()) { flag = false; } } Myadapter.this.data.get(groupPosition).setPresenterck(flag); iview.heji(); notifyDataSetChanged(); } }); mychildhodler.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int count = mybean.getCount(); mybean.setCount(++count); iview.heji(); notifyDataSetChanged(); } }); mychildhodler.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int count = mybean.getCount(); if (count > 0) { count--; mybean.setCount(count); } else { Toast.makeText(context, "不能再减了", Toast.LENGTH_SHORT).show(); } iview.heji(); notifyDataSetChanged(); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class Mypresenthodler { public CheckBox prenter_cb; public TextView prenter_tv; public Mypresenthodler(CheckBox prenter_cb, TextView prenter_tv) { this.prenter_cb = prenter_cb; this.prenter_tv = prenter_tv; } } class Mychildhodler { public CheckBox child_ck; public SimpleDraweeView child_sd; public TextView cjild_name; public TextView cjild_price; public ImageView jian; public EditText ed; public ImageView jia; public Mychildhodler(CheckBox child_ck, SimpleDraweeView child_sd, TextView cjild_name, TextView cjild_price, ImageView jian, EditText ed, ImageView jia) { this.child_ck = child_ck; this.child_sd = child_sd; this.cjild_name = cjild_name; this.cjild_price = cjild_price; this.jian = jian; this.ed = ed; this.jia = jia; } } }
main 主方法
public class MainActivity extends AppCompatActivity implements Iview { @BindView(R.id.tv) TextView tv; @BindView(R.id.edlist) ExpandableListView edlist; @BindView(R.id.cb) CheckBox cb; @BindView(R.id.hj) TextView hj; @BindView(R.id.jiesuan) Button jiesuan; @BindView(R.id.bianji) LinearLayout bianji; @BindView(R.id.shanchu) Button shanchu; @BindView(R.id.wancheng) LinearLayout wancheng; boolean flog = true; private Listdata; private Myadapter myadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); Mypresenter mypresenter = new Mypresenter(this, this); mypresenter.getmv(); wancheng.setVisibility(View.GONE); //全选的点击事件 quanxuan(cb); //编辑的点击事件 tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (flog) { tv.setText("完成"); flog = false; wancheng.setVisibility(View.VISIBLE); bianji.setVisibility(View.GONE); } else { tv.setText("编辑"); flog = true; bianji.setVisibility(View.VISIBLE); wancheng.setVisibility(View.GONE); } } }); //去除默认指示器 edlist.setGroupIndicator(null); //删除的事件 shanchu(); edlist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { return true; } }); } private void shanchu() { shanchu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //data.size()需要在上面定义 for (int i = 0; i < data.size(); i++) { List list = data.get(i).getList(); boolean presenterck = data.get(i).isPresenterck(); if (presenterck) { data.remove(i); heji(); //myadapter也需要在上面定义 myadapter.notifyDataSetChanged(); } for (int j = 0; j < list.size(); j++) { boolean childck = list.get(j).isChildck(); if (childck) { list.remove(j); heji(); myadapter.notifyDataSetChanged(); } } } } }); } private void quanxuan(CheckBox cb) { cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < data.size(); i++) { boolean presenterck = data.get(i).isPresenterck(); data.get(i).setPresenterck(!presenterck); List list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { boolean childck = list.get(j).isChildck(); list.get(j).setChildck(!childck); } } heji(); myadapter.notifyDataSetChanged(); } }); } @Override public void setadapter(Context context, List data) { this.data = data; myadapter = new Myadapter(context, data, this); edlist.setAdapter(myadapter); //defaultExpand实在下面 二级列表默认展开 defaultExpand(); } @Override public void onFailed(String msg) { } //二级列表默认展开 private void defaultExpand() { for (int i = 0; i < myadapter.getGroupCount(); ++i) { edlist.expandGroup(i); } } @Override public void heji() { double add = 0; int count = 0; for (int i = 0; i < data.size(); i++) { List list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { boolean childck = list.get(j).isChildck(); if (childck) { count++; int mycount = list.get(j).getCount() + 1; //这地方错需要在下面写 double mul = mul(list.get(j).getPrice(), mycount); add = add(add, mul); } } } hj.setText(add + ""); jiesuan.setText("去结算" + "(" + count + ")"); //跳转页面 需要在这里写点击事件,用Intent跳转 } //乘法 public static double mul(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); } //加法 public static double add(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } }
utils 包下 Getnet 的类
public class Getnet { public static String net="http://120.27.23.105/"; }
utils 包下的 MyAPP 的类
public class Myapp extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); Fresco.initialize(this); } }
utils 包下的 Testservice 的接口
public interface Testservice {
//需要换成自己的uid 10192 @GET("product/getCarts?source=android&uid=71") Observablegetuser(); }
utils 包下 Util 的类
public class Util { private volatile static Util util = null; private Util() { } public static Util getmInstance() { if (util == null) { synchronized (Util.class) { if (util == null) { util = new Util(); } } } return util; } public Testservice getnetjson(String uri) { Retrofit retrofit = new Retrofit.Builder().baseUrl(uri) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); Testservice testservive = retrofit.create(Testservice.class); return testservive; }
}
清单文件
android:name=".utils.Myapp"