首先导入依赖
implementation 'com.squareup.picasso:picasso:2.3.2'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
布局
activity_main布局
<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=".MainActivity">
<ExpandableListView
android:id="@+id/expanded_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="#c97373"
android:gravity="center_horizontal">
<CheckBox
android:id="@+id/ck_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tv_heji"
android:layout_weight="1"
android:paddingLeft="20dp"
android:text="合计:$0.00"/>
<Button
android:background="#ee0707"
android:text="去结算(0)"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
jia_jian布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#a19999"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jia_jian"
android:layout_marginLeft="5dp"
android:gravity="center"
android:text="-"
android:textColor="#fff"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jia"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jia_jia"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="+"
android:textColor="#fff"
android:textSize="20sp"
/>
</LinearLayout>
main_child布局
<?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="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/child_ck"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher_round"
android:layout_width="100dp"
android:layout_height="100dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/child_name"
android:text="描述"
android:layout_width="130dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/child_price"
android:text="价格"/>
</LinearLayout>
<com.bw.lly.gouwuche3.view.AddView
android:id="@+id/addview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:background="#2db6dc"
/>
</LinearLayout>
main_group布局
<?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="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/group_ck"
android:focusable="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/group_tv"
android:text="商品"
android:textSize="20sp"/>
</LinearLayout>
User类
import java.util.List;
public class User {
/**
* msg : 请求成功
* code : 0
* data : [{"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":2,"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":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":63,"price":10000,"pscid":40,"selected":0,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","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":15,"price":233.99,"pscid":1,"selected":0,"sellerid":8,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家8","sellerid":"8"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":99,"price":2100,"pscid":112,"selected":0,"sellerid":10,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"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":17,"price":299,"pscid":1,"selected":0,"sellerid":10,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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":66,"price":13000,"pscid":40,"selected":0,"sellerid":10,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家10","sellerid":"10"}]
*/
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":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":2,"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
*/
private String sellerName;
private String sellerid;
private boolean isChecked;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
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 : 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 : 2
* pid : 45
* price : 2999
* pscid : 39
* selected : 0
* sellerid : 1
* subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!
* title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机
*/
private String bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private String pid;
private String price;
private String pscid;
private String selected;
private String sellerid;
private String subhead;
private String title;
private boolean isCheck;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public String getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(String 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 Integer getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getPscid() {
return pscid;
}
public void setPscid(String pscid) {
this.pscid = pscid;
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String 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;
}
}
}
}
OKhttpUrl层
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class OkHttpUrl {
private static final String METHOD_GET = "GET";
private static OkHttpClient okHttpClient;
public OkHttpUrl() {
}
public static void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(3000, TimeUnit.MILLISECONDS);
builder.writeTimeout(3000, TimeUnit.MILLISECONDS);
okHttpClient = builder.build();
}
public static Request createGet(String url, String method) {
Request.Builder builder = new Request.Builder()
.url(url);
Request request = null;
switch (method) {
case METHOD_GET:
request = builder.build();
break;
}
return request;
}
public static void enenwue(String url, Callback callback) {
Request request = createGet(url, METHOD_GET);
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
UrlView层
public interface UrlView {
void onSuccess(String result);
void onFailer(String msg);
}
UrlModel层
import android.os.Handler;
import android.os.Message;
import com.bw.lly.gouwuche3.url.OkHttpUrl;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class UrlModel {
public void urlModel(String path, final HttpUrll httpUrl){
OkHttpUrl.enenwue(path, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
final String s = body.string();
handler.post(new Runnable() {
@Override
public void run() {
httpUrl.getName(s);
}
});
}
});
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
public interface HttpUrll{
void getName(String name);
}
}
UrlPresenter层
import com.bw.lly.gouwuche3.model.UrlModel;
import com.bw.lly.gouwuche3.view.UrlView;
public class UrlPresenter {
private UrlView urlView;
private final UrlModel urlModel;
public UrlPresenter(UrlView urlView) {
this.urlView = urlView;
urlModel = new UrlModel();
}
public void urlPresenter(String path){
urlModel.urlModel(path, new UrlModel.HttpUrll() {
@Override
public void getName(String name) {
if (name!=null){
urlView.onSuccess(name);
}else {
urlView.onFailer("失败");
}
}
});
}
}
Addview层
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AddView extends LinearLayout {
private View rootView;
private TextView jia_jian;
private TextView jia;
private TextView jia_jia;
public AddView(Context context) {
this(context, null);
}
public AddView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public AddView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//找控件
initView(context);
//监听
initListener();
}
private void initListener() {
//加
jia_jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});
//减
jia_jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
jian();
}
});
}
private void initView(Context context) {
rootView = View.inflate(context, R.layout.jia_jian, this);
jia_jian = rootView.findViewById(R.id.jia_jian);
jia_jia = rootView.findViewById(R.id.jia_jia);
jia = rootView.findViewById(R.id.jia);
jia.setText("1");
}
private void add() {
String s = jia.getText().toString();
int i = Integer.parseInt(s);
i++;
setCurrentCount(i);
}
private void jian() {
String s = jia.getText().toString();
int i = Integer.parseInt(s);
if (i > 1) {
i--;
setCurrentCount(i);
} else {
Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
}
public void setCurrentCount(int i) {
jia.setText("" + i);
if (onNumChangeListener!=null){
onNumChangeListener.onNumChanged(this,i);
}
}
//接口
private OnNumChangeListener onNumChangeListener;
public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
this.onNumChangeListener = onNumChangeListener;
}
public interface OnNumChangeListener {
void onNumChanged(View view, int curNum);
}
}
ExpandAdapter适配器
public class ExpandAdapter extends BaseExpandableListAdapter {
private List<User.DataBean> list;
private Context context;
private AddView.OnNumChangeListener onNumChangeListener;
public ExpandAdapter(List<User.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
//group数目
@Override
public int getGroupCount() {
return list.size();
}
//child数目
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
//group数据
@Override
public Object getGroup(int i) {
return list.get(i);
}
//child数据
@Override
public Object getChild(int i, int i1) {
return list.get(i).getList().get(i1);
}
//group个数
@Override
public long getGroupId(int i) {
return i;
}
//child个数
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
//group布局view
@Override
public View getGroupView(int groupPostion, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.main_group, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.groupck = convertView.findViewById(R.id.group_ck);
groupViewHolder.groupName = convertView.findViewById(R.id.group_tv);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
//赋值
groupViewHolder.groupName.setText(list.get(groupPostion).getSellerName());
groupViewHolder.groupck.setChecked(list.get(groupPostion).isChecked());
return convertView;
}
//child布局
@Override
public View getChildView(final int groupPosition, final int childPostion, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.main_child, null);
childViewHolder = new ChildViewHolder();
childViewHolder.childck = convertView.findViewById(R.id.child_ck);
childViewHolder.childName = convertView.findViewById(R.id.child_name);
childViewHolder.addView = convertView.findViewById(R.id.addview);
childViewHolder.childImage = convertView.findViewById(R.id.image);
childViewHolder.childPrice=convertView.findViewById(R.id.child_price);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//赋值
String images = list.get(groupPosition).getList().get(childPostion).getImages();
String[] split = images.split("!");
if (split.length > 0) {
Picasso.with(context).load(split[0]).into(childViewHolder.childImage);
}
childViewHolder.childName.setText(list.get(groupPosition).getList().get(childPostion).getTitle());
childViewHolder.childPrice.setText(list.get(groupPosition).getList().get(childPostion).getPrice());
childViewHolder.childck.setChecked(list.get(groupPosition).getList().get(childPostion).isCheck());
childViewHolder.addView.setOnNumChangeListener(new AddView.OnNumChangeListener() {
@Override
public void onNumChanged(View view, int curNum) {
list.get(groupPosition).getList().get(childPostion).setNum(curNum);
if (onNumChangeListener != null) {
onNumChangeListener.onNumChanged(view, curNum);
}
}
});
return convertView;
}
public void setOnNumChangeListener(AddView.OnNumChangeListener onNumChangeListener) {
this.onNumChangeListener = onNumChangeListener;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
//group
class GroupViewHolder {
CheckBox groupck;
TextView groupName;
}
//child
class ChildViewHolder {
CheckBox childck;
ImageView childImage;
TextView childName;
TextView childPrice;
AddView addView;
}
}
App
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
OkHttpUrl.init();
}
}
MainActivity主页面
public class MainActivity extends AppCompatActivity implements UrlView {
private ExpandableListView expandableListView;
private CheckBox chAll;
private TextView tvSum;
private UrlPresenter urlPresenter;
private String path="http://120.27.23.105/product/getCarts?source=android&uid=99";
private List<User.DataBean> list;
private ExpandAdapter expandAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
expandableListView = findViewById(R.id.expanded_view);
chAll = findViewById(R.id.ck_all);
tvSum = findViewById(R.id.tv_heji);
//网络请求
urlPresenter = new UrlPresenter(this);
urlPresenter.urlPresenter(path);
//选中
chAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
setCheckAll();
}
});
}
//====================全选======================
private void setCheckAll() {
int groupCount = expandAdapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean user= (User.DataBean) expandAdapter.getGroup(i);
List<User.DataBean.ListBean> itemlist = user.getList();
for (int j = 0; j < itemlist.size(); j++) {
User.DataBean.ListBean listBean = itemlist.get(j);
if (chAll.isChecked()){
user.setChecked(true);
listBean.setCheck(true);
}else {
user.setChecked(false);
listBean.setCheck(false);
}
}
}
expandAdapter.notifyDataSetChanged();
getTotal();
}
//==========================价格总价======================
private void getTotal() {
float total=0;
int groupCount = expandAdapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean user = (User.DataBean) expandAdapter.getGroup(i);
List<User.DataBean.ListBean> list = user.getList();
for (int j = 0; j < list.size(); j++) {
User.DataBean.ListBean listBean = list.get(j);
boolean check = listBean.isCheck();
if (check==true){
String price = listBean.getPrice();
total=total+(Float.valueOf(price)*listBean.getNum());
}
}
}
tvSum.setText("合计:"+total);
}
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
User user = gson.fromJson(result, User.class);
list=user.getData();
//设置适配器
expandAdapter = new ExpandAdapter(list,this);
expandableListView.setAdapter(expandAdapter);
for (int i = 0; i < expandAdapter.getGroupCount(); i++) {
expandableListView.expandGroup(i);
}
initShopcarChange();
}
private void initShopcarChange() {
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
User.DataBean group = (User.DataBean) expandAdapter.getGroup(groupPosition);
//让group中的CheckBox选中,之类就跟着选中
group.setChecked(!group.isChecked());
boolean flag=true;
if (group.isChecked()){
flag=false;
}
List<User.DataBean.ListBean> list = group.getList();
for (int i = 0; i < list.size(); i++) {
User.DataBean.ListBean listBean = list.get(i);
listBean.setCheck(!flag);
}
expandAdapter.notifyDataSetChanged();
getTotal();
return true;
}
});
//子类的合计
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
User.DataBean.ListBean child = (User.DataBean.ListBean) expandAdapter.getChild(groupPosition, childPosition);
boolean check = child.isCheck();
if (check){
child.setCheck(true);
}else{
child.setCheck(false);
}
expandAdapter.notifyDataSetChanged();
getTotal();
return true;
}
});
expandAdapter.setOnNumChangeListener(new AddView.OnNumChangeListener() {
@Override
public void onNumChanged(View view, int curNum) {
getTotal();
}
});
}
@Override
public void onFailer(String msg) {
Toast.makeText(getApplicationContext(),"解析失败",Toast.LENGTH_SHORT).show();
}
}