2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:id="@+id/tv_heji"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:text="合计:$0.00" />
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="去结算(0)" />
</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:layout_height="wrap_content"
android:background="#a19999"
android:orientation="horizontal">
<TextView
android:id="@+id/jia_jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center"
android:text="-"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/jia_jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="@+id/child_name"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="描述" />
<TextView
android:id="@+id/child_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="价格" />
</LinearLayout>
<com.umeng.soexmple.goshooping01.view.AddView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:id="@+id/addView"></com.umeng.soexmple.goshooping01.view.AddView>
</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:id="@+id/group_ck"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/group_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="商品" />
</LinearLayout>
3.view层
public interface ShopView {
void onSuccess(String result);
void onFailure(String msg);
}
//========addview自定义view
public class AddView extends LinearLayout {
private View rootview;
private TextView jia_jian;
private TextView jia_jia;
private TextView jia;
public AddView(Context context) {
this(context, null);
}
public AddView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public AddView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//找控件
initView(context);
//监听
initListener();
}
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 initListener() {
//加
jia_jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add();
}
});
//减
jia_jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jian();
}
});
}
public void add(){
String s = jia.getText().toString();
int i = Integer.parseInt(s);
i++;
setCurrentCount(i);
}
public void jian(){
String s = jia.getText().toString();
int i = Integer.parseInt(s);
if(i>1){
i--;
setCurrentCount(i);
}else{
Toast.makeText(getContext(),"不能再减少了",Toast.LENGTH_LONG).show();
}
}
public void setCurrentCount(int i){
jia.setText(""+i);
if(onNumChangeListener!=null){
onNumChangeListener.onNumChanged(this,i);
}
}
private onNumChangeListener onNumChangeListener;
public void setOnNumChangeListener(AddView.onNumChangeListener onNumChangeListener) {
this.onNumChangeListener = onNumChangeListener;
}
//接口
public interface onNumChangeListener{
void onNumChanged(View view,int curNum);
}
}
4.model层
public class ShopModel {
public void shop(String path, final HttpUrl httpUrl){
OkHttpUtils.enqueue(path, new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String s = response.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 HttpUrl{
void getName(String name);
}
}
5.persenter
public class ShopPersenter {
private ShopModel shopModel;
private ShopView shopView;
public ShopPersenter(ShopView shopView) {
shopModel = new ShopModel();
this.shopView = shopView;
}
public void urlPresenter(String path){
shopModel.shop(path, new ShopModel.HttpUrl() {
@Override
public void getName(String name) {
if(name!=null){
shopView.onSuccess(name);
}else{
shopView.onFailure("失败");
}
}
});
}
}
6.okhttp
public class OkHttpUtils {
private static final String METHOD_GET = "GET";
private static OkHttpClient client;
public OkHttpUtils() {
}
public static void init(){
//创建
client = new OkHttpClient.Builder()
.writeTimeout(3000,TimeUnit.MILLISECONDS)
.readTimeout(3000,TimeUnit.MILLISECONDS)
.build();
}
public static Request createGet(String url,String method){
//request请求
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 enqueue(String url, Callback callback){
Request request = createGet(url, METHOD_GET);
Call call = client.newCall(request);
call.enqueue(callback);
}
}
7.bean类
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;
private boolean isCheck;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
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;
}
}
}
}
8.app初始化网络请求
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
OkHttpUtils.init();
}
}
9.adapter
public class ExpandAdapter extends BaseExpandableListAdapter {
private List<User.DataBean> list=new ArrayList<>();
private Context context;
private AddView.onNumChangeListener onNumChangeListener;
public ExpandAdapter(List<User.DataBean> list, Context context) {
if(list!=null){
this.list.addAll(list);
}
this.context = context;
}
//group数目
@Override
public int getGroupCount() {
return list.size();
}
//child数目
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList().size();
}
//group数据
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
//child数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getList().get(childPosition);
}
//group个数
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//child个数
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//group布局
@Override
public View getGroupView(int groupPosition, 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(groupPosition).getSellerName());
groupViewHolder.groupCK.setChecked(list.get(groupPosition).isChecked());
return convertView;
}
//child布局
@Override
public View getChildView(final int groupPosition, final int childPosition, 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.childImage = convertView.findViewById(R.id.image);
childViewHolder.childName = convertView.findViewById(R.id.child_name);
childViewHolder.childPrice = convertView.findViewById(R.id.child_price);
childViewHolder.addView = convertView.findViewById(R.id.addView);
convertView.setTag(childViewHolder);
}else{
childViewHolder= (ChildViewHolder) convertView.getTag();
}
//图片
String images=list.get(groupPosition).getList().get(childPosition).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(childPosition).getTitle());
childViewHolder.childPrice.setText(list.get(groupPosition).getList().get(childPosition).getPrice());
childViewHolder.childck.setChecked(list.get(groupPosition).getList().get(childPosition).isCheck());
childViewHolder.addView.setOnNumChangeListener(new AddView.onNumChangeListener() {
@Override
public void onNumChanged(View view, int curNum) {
list.get(groupPosition).getList().get(childPosition).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 groupPosition, int childPosition) {
return true;
}
//group viewholder
class GroupViewHolder{
CheckBox groupCK;
TextView groupName;
}
//child viewholder
class ChildViewHolder{
CheckBox childck;
ImageView childImage;
TextView childName;
TextView childPrice;
AddView addView;
}
}
10mainactivity
public class MainActivity extends AppCompatActivity implements ShopView {
private ExpandableListView expandableListView;
private CheckBox ck_all;
private TextView tvsum;
private String path = "http://120.27.23.105/product/getCarts?source=android&uid=99";
private List<User.DataBean> list;
private ExpandAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
expandableListView = findViewById(R.id.expanded_view);
ck_all = findViewById(R.id.ck_all);
tvsum = findViewById(R.id.tv_heji);
//网络请求
ShopPersenter shopPersenter = new ShopPersenter(this);
shopPersenter.urlPresenter(path);
//选中
ck_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setCheckAll();
}
});
}
//主页面的全选
private void setCheckAll(){
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean user = (User.DataBean) adapter.getGroup(i);
List<User.DataBean.ListBean> list = user.getList();
for (int j = 0; j <list.size() ; j++) {
User.DataBean.ListBean listBean = list.get(j);
if(ck_all.isChecked()){
user.setCheck(true);
listBean.setCheck(true);
}
else{
user.setCheck(false);
listBean.setCheck(false);
}
}
}
adapter.notifyDataSetChanged();
getTotal();
}
//价格总计
private void getTotal(){
float total=0;
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
User.DataBean user= (User.DataBean) adapter.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();
//设置适配器
adapter = new ExpandAdapter(list,this);
expandableListView.setAdapter(adapter);
for (int i = 0; i <adapter.getGroupCount() ; i++) {
expandableListView.expandGroup(i);
}
initShopcartChange();
}
private void initShopcartChange() {
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
User.DataBean group = (User.DataBean) adapter.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);
}
adapter.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) adapter.getChild(groupPosition, childPosition);
boolean check = child.isCheck();
if(check){
child.setCheck(true);
}else{
child.setCheck(false);
}
adapter.notifyDataSetChanged();
getTotal();
return true;
}
});
adapter.setOnNumChangeListener(new AddView.onNumChangeListener() {
@Override
public void onNumChanged(View view, int curNum) {
getTotal();
}
});
}
@Override
public void onFailure(String msg) {
Toast.makeText(this,"解析失败",Toast.LENGTH_LONG).show();
}
}
11.网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
application注册
android:name="App"