加依赖
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
布局
drawable 里 biankuang.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"></solid>
<stroke android:width="0.1dp"
android:color="#000000"/>
</shape>
drawable 里 ic_launcher_background.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillColor="#26A69A"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
</vector>
主布局 activity_main.xml
<?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.example.administrator.day_22.View.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:textSize="25sp"
android:text="购物车"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_marginTop="5px"
android:background="#7575"
android:layout_width="match_parent"
android:layout_height="0.75px"/>
<ExpandableListView
android:id="@+id/elv"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ExpandableListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:gravity="center_vertical"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60px">
<CheckBox
android:id="@+id/qx"
android:layout_marginLeft="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="20sp"
android:text="全选/反选"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/zj"
android:layout_marginLeft="15px"
android:textColor="#ff00"
android:text="总价:"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ff00"
android:layout_marginLeft="50dp"
android:text="去结算:"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
child_item.xml
<?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="horizontal">
<CheckBox
android:id="@+id/c_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/c_iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_marginLeft="10dp"
android:textSize="22sp"
android:id="@+id/c_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="55555"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:id="@+id/c_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="555555"/>
<TextView
android:layout_marginLeft="20sp"
android:textSize="20sp"
android:padding="3dp"
android:text="-"
android:background="@drawable/biankuang"
android:id="@+id/c_jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="20sp"
android:id="@+id/c_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="0"/>
<TextView
android:textSize="20sp"
android:padding="3dp"
android:text="+"
android:background="@drawable/biankuang"
android:id="@+id/c_jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:text="删除"
android:id="@+id/c_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
group_item.xml
<?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="horizontal">
<CheckBox
android:id="@+id/g_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/g_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Bean
LiebiaoBean.java
package com.example.administrator.day_22.Bean;
import java.util.List;
/**
* Created by dell on 2018/1/17.
*/
public class LiebiaoBean {
/**
* 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":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","num":1,"pid":59,"price":5599,"pscid":40,"selected":0,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","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":71,"price":32999,"pscid":40,"selected":0,"sellerid":15,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家15","sellerid":"15"},{"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":72,"price":33999,"pscid":40,"selected":0,"sellerid":16,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家16","sellerid":"16"}]
*/
private String msg;
private String code;
private List<DataBean> data;
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<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* 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 boolean grooup_flag;
public boolean isGrooup_flag() {
return grooup_flag;
}
public void setGrooup_flag(boolean grooup_flag) {
this.grooup_flag = grooup_flag;
}
private String sellerName;
private String sellerid;
private List<ListBean> 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<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
/**
* 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)银
*/
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 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 int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int 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;
}
}
}
}
Model层
iModel.java
package com.example.administrator.day_22.Model;
import java.util.Map;
/**
* Created by dell on 2018/1/17.
*/
public interface iModel {
//获取列表数据的方法
void getLieBiao(String url, Map<String, String> map, OnModelListener onModelListener);
//刷新购物车的方法
void ShuaCart(String url, Map<String, String> map, OnModelListener onModelListener);
//删除
void DeleteCart(String url, Map<String, String> map, OnModelListener onModelListener);
}
iModelImpl.java
package com.example.administrator.day_22.Model;
import com.example.administrator.day_22.NetUtil.MyOkHttp;
import com.example.administrator.day_22.NetUtil.MyOkHttpListener;
import java.util.Map;
/**
* Created by dell on 2018/1/17.
*/
public class iModelImpl implements iModel {
//列表
@Override
public void getLieBiao(String url, Map<String, String> map, final OnModelListener onModelListener) {
MyOkHttp danli = MyOkHttp.danli();
danli.setMyOkHttpListener(new MyOkHttpListener() {
@Override
public void success(String json) {
onModelListener.Success(json);
}
@Override
public void error(String error) {
onModelListener.Error(error);
}
});
danli.PostOkHttp(url,map);
}
@Override
public void ShuaCart(String url, Map<String,String>map, final OnModelListener onModelListener) {
MyOkHttp danli = MyOkHttp.danli();
danli.setMyOkHttpListener(new MyOkHttpListener() {
@Override
public void success(String json) {
onModelListener.Success(json);
}
@Override
public void error(String error) {
onModelListener.Error(error);
}
});
danli.PostOkHttp(url,map);
}
@Override
public void DeleteCart(String url, Map<String, String> map, final OnModelListener onModelListener) {
MyOkHttp danli = MyOkHttp.danli();
danli.setMyOkHttpListener(new MyOkHttpListener() {
@Override
public void success(String json) {
onModelListener.Success(json);
}
@Override
public void error(String error) {
onModelListener.Error(error);
}
});
danli.PostOkHttp(url,map);
}
}
OnModelListener.java
package com.example.administrator.day_22.Model;
public interface OnModelListener {
//成功的方法
void Success(String json);
//失败的方法
void Error(String error);
}
MyAdaper 层
ExAdaper.java
package com.example.administrator.day_22.MyAdaper;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.administrator.day_22.Bean.LiebiaoBean;
import com.example.administrator.day_22.Model.iModelImpl;
import com.example.administrator.day_22.Presenter.iPresenterImpl;
import com.example.administrator.day_22.R;
import com.example.administrator.day_22.View.IView;
import java.util.ArrayList;
import java.util.List;
public class ExAdaper extends BaseExpandableListAdapter {
List<LiebiaoBean.DataBean> data;
Context context;
iPresenterImpl p;
List<LiebiaoBean.DataBean.ListBean> list=new ArrayList<>();
public ExAdaper(iPresenterImpl p,List<LiebiaoBean.DataBean> data, Context context) {
this.data = data;
this.context = context;
this.p=p;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int i) {
if (data.size()==0){
return 0;
}
return data.get(i).getList().size();
}
@Override
public Object getGroup(int i) {
return data.get(i);
}
@Override
public Object getChild(int i, int i1) {
return data.get(i).getList().get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final MyHolder1 h1;
if(view==null){
view=View.inflate(context, R.layout.group_item,null);
h1 = new MyHolder1();
h1.g_cb= (CheckBox) view.findViewById(R.id.g_cb);
h1.g_tv= (TextView) view.findViewById(R.id.g_tv);
view.setTag(h1);
}else{
h1= (MyHolder1) view.getTag();
}
//因为会重新调用,接口中没有isGrooup_flag,这个属性,
// 所以有BUG,直接遍历子元素,如果都选中,则父元素选中
h1.g_cb.setChecked(setGroup(i));
h1.g_tv.setText(data.get(i).getSellerName());
//父点击选择改变子
h1.g_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//改变父元素的选择状态,用于改变子元素
data.get(i).setGrooup_flag(!data.get(i).isGrooup_flag());
for (int j = 0; j <data.get(i).getList().size(); j++) {
if(data.get(i).isGrooup_flag()){
data.get(i).getList().get(j).setSelected(1);
}else{
data.get(i).getList().get(j).setSelected(0);
}
}
//data.get(i).getList() 当前父元素的所以孩子,用于递归刷新接口数据
//掉刷新的接口方法
p.ShuaCartIPrensenter(0,data.get(i).getList(),new iModelImpl(), (IView) context);
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final MyHolder2 h2;
if(view==null){
view=View.inflate(context,R.layout.child_item,null);
h2=new MyHolder2();
h2.c_cb= (CheckBox) view.findViewById(R.id.c_cb);
h2.c_iv= (ImageView) view.findViewById(R.id.c_iv);
h2.c_title= (TextView) view.findViewById(R.id.c_title);
h2.c_price= (TextView) view.findViewById(R.id.c_price);
h2.c_jian= (TextView) view.findViewById(R.id.c_jian);
h2.c_count= (TextView) view.findViewById(R.id.c_count);
h2.c_jia= (TextView) view.findViewById(R.id.c_jia);
h2.c_delete= (TextView) view.findViewById(R.id.c_delete);
view.setTag(h2);
}else{
h2= (MyHolder2) view.getTag();
}
h2.c_cb.setChecked(data.get(i).getList().get(i1).getSelected()==1?true:false);
String images = data.get(i).getList().get(i1).getImages();
String s = images.split(".jpg")[0] + ".jpg";
Glide.with(context).load(s).into( h2.c_iv);
h2.c_title.setText(data.get(i).getList().get(i1).getTitle());
h2.c_price.setText("优惠价: ¥"+data.get(i).getList().get(i1).getNum()*data.get(i).getList().get(i1).getBargainPrice()+"");
h2.c_count.setText(data.get(i).getList().get(i1).getNum()+"");
//子条目点击事件
h2.c_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(data.get(i).getList().get(i1).getSelected()==0){
data.get(i).getList().get(i1).setSelected(1);
}else{
data.get(i).getList().get(i1).setSelected(0);
}
//刷新接口
p.ShuaCartIPrensenter(0,data.get(i).getList(),new iModelImpl(), (IView) context);
}
});
//减的点击事件
h2.c_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = data.get(i).getList().get(i1).getNum();
if(num==0){
return;
}
data.get(i).getList().get(i1).setNum(--num);
//刷新接口
p.ShuaCartIPrensenter(0,data.get(i).getList(),new iModelImpl(), (IView) context);
}
});
//加的点击事件
h2.c_jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = data.get(i).getList().get(i1).getNum();
data.get(i).getList().get(i1).setNum(++num);
//刷新接口
p.ShuaCartIPrensenter(0,data.get(i).getList(),new iModelImpl(), (IView) context);
}
});
//删除的点击事件
h2.c_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//调删除的接口
LiebiaoBean.DataBean.ListBean listBean = data.get(i).getList().get(i1);
p.DeleterCartPresenter(listBean,new iModelImpl(), (IView) context);
//直接删除集合的数据
data.get(i).getList().remove(i1);
//判读子有没有,如果没有就删除父
if(data.get(i).getList().size()==0){
data.remove(i);
}
}
});
//判断如果孩子全选了就要选择父元素
data.get(i).setGrooup_flag(setGroup(i));
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
//遍历所以子元素,给父元素赋值选择状态,看是否全部选中
private boolean setGroup(int i){
for (int j = 0; j <data.get(i).getList().size(); j++) {
if(data.get(i).getList().get(j).getSelected()==0){
return false;
}
}
return true;
}
class MyHolder1{
CheckBox g_cb;
TextView g_tv;
}
class MyHolder2{
CheckBox c_cb;
ImageView c_iv;
TextView c_title;
TextView c_price;
TextView c_jian;
TextView c_count;
TextView c_jia;
TextView c_delete;
}
}
NetUtil 层
HttpConfig.java
package com.example.administrator.day_22.NetUtil;
public class HttpConfig {
//商品详情地址
public static String liebiao_url="https://www.zhaoapi.cn/product/getCarts";
//刷新购物车
public static String shuacart_url="https://www.zhaoapi.cn/product/updateCarts";
//删除的接口
public static String delete_url="https://www.zhaoapi.cn/product/deleteCart";
}
MyOkHttp.java
package com.example.administrator.day_22.NetUtil;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MyOkHttp {
//单例模式
private static MyOkHttp myOkHttp=null;
MyHandler myHandler=new MyHandler();
MyOkHttpListener myOkHttpListener;
public static MyOkHttp danli(){
if(myOkHttp==null){
myOkHttp=new MyOkHttp();
}
return myOkHttp;
}
//get
public void getOkHttp(String url){
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message msg=new Message();
msg.what=0;
msg.obj=e.toString();
myHandler.sendMessage(msg);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message msg=new Message();
msg.what=1;
msg.obj=response.body().string();
myHandler.sendMessage(msg);
}
});
}
//post
public void PostOkHttp(String url, Map<String,String> map){
OkHttpClient client=new OkHttpClient.Builder().addInterceptor(new MyInter()).build();
//创建FromBody
FormBody.Builder builder=new FormBody.Builder();
//遍历map
Set<String> keys = map.keySet();
for (String key:keys) {
String v = map.get(key);
builder.add(key,v);
}
//build
FormBody build = builder.build();
Request request=new Request.Builder().url(url).post(build).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message msg=new Message();
msg.what=0;
msg.obj=e.toString();
myHandler.sendMessage(msg);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message msg=new Message();
msg.what=1;
msg.obj=response.body().string();
myHandler.sendMessage(msg);
}
});
}
//更新头像的方法
private static final String TAG = "MyOkHttp----";
public void uploadFile(String url, File file,Map<String, String> map,Uri data2){
OkHttpClient okHttpClient = new OkHttpClient();
//MultipartBody 传图片 二进制的信息要用这个
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
//遍历map 获取参数添加到builder中
Set<String> keys = map.keySet();
for (String key:keys) {
String v = map.get(key);
builder.addFormDataPart(key,v);
}
//把文件对象放到请求路径中
builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
//创建 build
MultipartBody requestBody = builder.build();
Request request = new Request.Builder()
.post(requestBody)//请求体
.url(url)//指定路径 就是请求的接口
.build();//启动
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "失败---" );
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("aaa","成功");
}
});
}
//拦截器
class MyInter implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//获取原来的body
Request request = chain.request();
RequestBody body = request.body();
if(body instanceof FormBody){
//遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body
FormBody.Builder newBuilder=new FormBody.Builder();
for (int i = 0; i <((FormBody) body).size() ; i++) {
String name = ((FormBody) body).name(i);
String value = ((FormBody) body).value(i);
//放入新的
newBuilder.add(name,value);
}
//在将公共参数添加进去
FormBody newBody = newBuilder.add("source", "android").build();
//创建新的请求
Request build = request.newBuilder().post(newBody).build();
Response response = chain.proceed(build);
return response;
}
return null;
}
}
//外部访问的方法
public void setMyOkHttpListener(MyOkHttpListener myOkHttpListener){
this.myOkHttpListener=myOkHttpListener;
}
//Handler
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String error= (String) msg.obj;
myOkHttpListener.error(error);
break;
case 1:
String json= (String) msg.obj;
myOkHttpListener.success(json);
break;
}
}
}
}
MyOkHttpListener.java
package com.example.administrator.day_22.NetUtil;
public interface MyOkHttpListener {
//成功
void success(String json);
//失败
void error(String error);
}
Presenter层
iPresenter.java
package com.example.administrator.day_22.Presenter;
import com.example.administrator.day_22.Bean.LiebiaoBean;
import com.example.administrator.day_22.Model.iModel;
import com.example.administrator.day_22.View.IView;
import java.util.List;
public interface iPresenter {
//列表中间人
void LieBiaoRen(iModel imodel, IView iView);
//刷新购物车中间人
void ShuaCartIPrensenter(final int index, final List<LiebiaoBean.DataBean.ListBean> list, iModel imodel, final IView iView);
//删除购物车中间人
void DeleterCartPresenter(LiebiaoBean.DataBean.ListBean bean, iModel imodel, final IView iView);
}
iPresenterImpl.java
package com.example.administrator.day_22.Presenter;
import android.util.Log;
import com.example.administrator.day_22.Bean.LiebiaoBean;
import com.example.administrator.day_22.Model.OnModelListener;
import com.example.administrator.day_22.Model.iModel;
import com.example.administrator.day_22.NetUtil.HttpConfig;
import com.example.administrator.day_22.View.IView;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class iPresenterImpl implements iPresenter {
//列表中间人
@Override
public void LieBiaoRen(iModel imodel, final IView iView) {
Map<String,String> map=new HashMap<>();
map.put("uid","72");
imodel.getLieBiao(HttpConfig.liebiao_url, map, new OnModelListener() {
@Override
public void Success(String json) {
Gson g=new Gson();
LiebiaoBean liebiaoBean = g.fromJson(json, LiebiaoBean.class);
List<LiebiaoBean.DataBean> data = liebiaoBean.getData();
iView.SetView(data);
}
@Override
public void Error(String error) {
iView.Error(error);
}
});
}
@Override
public void ShuaCartIPrensenter(final int index, final List<LiebiaoBean.DataBean.ListBean> list, iModel imodel, final IView iView) {
Map<String,String> map=new HashMap<>();
map.put("uid","72");
map.put("sellerid",list.get(index).getSellerid()+"");
map.put("pid",list.get(index).getPid()+"");
map.put("selected",list.get(index).getSelected()+"");
map.put("num",list.get(index).getNum()+"");
imodel.ShuaCart(HttpConfig.shuacart_url, map, new OnModelListener() {
@Override
public void Success(String json) {
Log.e("shuaxin",json+"=====");
iView.ShuaCartView(index,list);
}
@Override
public void Error(String error) {
iView.Error(error);
}
});
}
//删除的方法
@Override
public void DeleterCartPresenter(LiebiaoBean.DataBean.ListBean bean, iModel imodel, final IView iView) {
Map<String,String> map=new HashMap<>();
map.put("uid","72");
map.put("pid",bean.getPid()+"");
map.put("token","DA76A53B7FECCCBD4594D8F47347A1B3");
imodel.DeleteCart(HttpConfig.delete_url,map,new OnModelListener(){
@Override
public void Success(String json){
Log.e("ASDASD",json);
iView.DeleteCartView();
}
@Override
public void Error(String error) {
iView.Error(error);
}
});
}
}
View层
IView.java
package com.example.administrator.day_22.View;
import com.example.administrator.day_22.Bean.LiebiaoBean;
import java.util.List;
public interface IView {
//展示页面的方法
void SetView(List<LiebiaoBean.DataBean> data);
//刷新购物车的方法
void ShuaCartView(int index, List<LiebiaoBean.DataBean.ListBean> list);
//删除的方法
void DeleteCartView();
//失败的方法
void Error(String error);
}
MainActivity.java
package com.example.administrator.day_22.View;
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 com.example.administrator.day_22.Bean.LiebiaoBean;
import com.example.administrator.day_22.Model.iModelImpl;
import com.example.administrator.day_22.MyAdaper.ExAdaper;
import com.example.administrator.day_22.Presenter.iPresenterImpl;
import com.example.administrator.day_22.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView,View.OnClickListener {
private ExpandableListView elv;
private iPresenterImpl p;
private TextView sum;
private ExAdaper ea;
List<LiebiaoBean.DataBean> data;
private CheckBox qx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化化控件
initViews();
p = new iPresenterImpl();
p.LieBiaoRen(new iModelImpl(),this);
}
private void initViews() {
elv = (ExpandableListView) findViewById(R.id.elv);
sum = (TextView) findViewById(R.id.zj);
qx = (CheckBox) findViewById(R.id.qx);
qx.setOnClickListener(this);
}
@Override
public void SetView(List<LiebiaoBean.DataBean> data) {
this.data=data;
//设置数据
ea = new ExAdaper(p,data, this);
elv.setAdapter(ea);
//设置总价
setZj();
//展开二级列表
for (int i = 0; i <data.size(); i++) {
elv.expandGroup(i);
}
}
public void setZj(){
double zj=0;
for (int i = 0; i <data.size(); i++) {
for (int j = 0; j <data.get(i).getList().size() ; j++) {
if(data.get(i).getList().get(j).getSelected()==1){
zj=zj+(data.get(i).getList().get(j).getBargainPrice()*data.get(i).getList().get(j).getNum());
}
}
}
zj=(int)((zj*100)/100);
sum.setText("总价: ¥"+zj);
}
//刷新购物车的方法
@Override
public void ShuaCartView(int index, List<LiebiaoBean.DataBean.ListBean> list) {
index=index+1;
if(index<list.size()){
p.ShuaCartIPrensenter(index,list,new iModelImpl(),this);
}else{
/*p.LieBiaoRen(new iModelImpl(),this);*/
//不用重新调用接口,直接刷新适配器即可,因为接口改变在调用会刷新页面
ea.notifyDataSetChanged();
//重新调取计算总价的方法
setZj();
}
}
@Override
public void DeleteCartView() {
//当删除集合数据之后,刷新适配器
ea.notifyDataSetChanged();
//重新调取计算总价的方法
setZj();
}
@Override
public void Error(String error) {
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.qx:
//点击全选是,遍历所有子元素,改变其状态
//新建集合用于放数据,去访问接口
List<LiebiaoBean.DataBean.ListBean> list =new ArrayList<>();
boolean aa = qx.isChecked();//获取全选的状态
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j <data.get(i).getList().size() ; j++) {
if(aa){
data.get(i).getList().get(j).setSelected(1);
}else{
data.get(i).getList().get(j).setSelected(0);
}
list.add(data.get(i).getList().get(j));
}
}
//调取刷新数据的接口
p.ShuaCartIPrensenter(0,list,new iModelImpl(),this);
break;
}
}
}