依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.3.1'
权限
<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:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.carlication.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ff3660" android:gravity="center" android:text="商品详情" android:textColor="#ffffff" android:textSize="25sp" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="400dp" /> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tvBargainPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" /> <TextView android:textColor="#F00" android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:id="@+id/tvCart" android:layout_width="0dp" android:background="#33000000" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="购物车" /> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="#000"/> <TextView android:id="@+id/tvAddCart" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#33000000" android:gravity="center" android:text="加入购物车" /> </LinearLayout> </LinearLayout >MainActivity
package com.example.carlication; import android.content.Intent; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.carlication.Bean.MoreBean; import com.example.carlication.Call.IAddCartView; import com.example.carlication.Call.INewsView; import com.example.carlication.Persenter.AddCartPersenter; import com.example.carlication.Persenter.NewsPersenter; public class MainActivity extends AppCompatActivity implements View.OnClickListener, INewsView, IAddCartView { private ImageView mIv; private TextView mTvBargainPrice; private TextView mTvPrice; /** * 购物车 */ private TextView mTvCart; /** * 加入购物车 */ private TextView mTvAddCart; private AddCartPersenter addCartPersenter; private TextView mTvTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); NewsPersenter presenter = new NewsPersenter(this); presenter.getNews(); addCartPersenter = new AddCartPersenter(this); } private void initView() { mIv = (ImageView) findViewById(R.id.iv); mTvBargainPrice = (TextView) findViewById(R.id.tvBargainPrice); mTvPrice = (TextView) findViewById(R.id.tvPrice); mTvCart = (TextView) findViewById(R.id.tvCart); mTvCart.setOnClickListener(this); mTvAddCart = (TextView) findViewById(R.id.tvAddCart); mTvAddCart.setOnClickListener(this); mTvTitle = (TextView) findViewById(R.id.tvTitle); } @Override public void onClick(View view) { switch (view.getId()) { default: break; case R.id.tvCart: Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); break; case R.id.tvAddCart: addCartPersenter.getNews(); break; } } @Override public void show(String code) { if (code.equals("0")) { Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_LONG).show(); ; } else { Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_LONG).show(); ; } } @Override public void failed(Exception e) { } @Override public void success(MoreBean.DataBean data) { String img = data.getImages(); if (img.contains("|")) { img = img.substring(0, img.indexOf("|")); } Log.i("imggg", img); Glide.with(this).load(img).into(mIv); mTvTitle.setText(data.getTitle()); mTvBargainPrice.setText("折扣价:" + data.getSalenum()); mTvPrice.setText("原价:" + data.getPrice()); mTvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); } }
PriceAndCount
package com.example.carlication; /** * Created by lenovo on 2017/12/15. */ public class PriceAndCount { private double price; private int count; public PriceAndCount(double price, int count) { this.price = price; this.count = count; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
AddCartPersenter
package com.example.carlication.Persenter; import com.example.carlication.Bean.AddCartBean; import com.example.carlication.Call.CallBack; import com.example.carlication.Call.IAddCartView; import com.example.carlication.Http.HttpsUrl; import java.util.HashMap; /** * Created by lenovo on 2017/12/15. */ public class AddCartPersenter { private IAddCartView iav; public AddCartPersenter(IAddCartView iav) { this.iav = iav; } public void getNews(){ HashMap<String , String> map = new HashMap<>(); map.put("source","android"); map.put("uid","39"); map.put("pid","71"); HttpsUrl.getInstance().get("https://www.zhaoapi.cn/product/addCart", map, new CallBack() { @Override public void onResponse(Object o) { AddCartBean addCartBean = (AddCartBean) o; if(addCartBean!=null){ String code = addCartBean.getCode(); iav.show(code); } } @Override public void onFailure(Exception e) { } },AddCartBean.class); } }
AddCartBean
package com.example.carlication.Bean; /** * Created by lenovo on 2017/12/15. */ public class AddCartBean { /** * msg : 加购成功 * code : 0 */ private String msg; private String code; 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; } }
HttpsUrl
package com.example.carlication.Http; import android.os.Handler; import android.text.TextUtils; import android.util.Log; import com.example.carlication.Call.CallBack; import com.google.gson.Gson; import java.io.IOException; import java.util.Map; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * Created by lenovo on 2017/12/15. */ public class HttpsUrl { private Handler handler = new Handler(); private static volatile HttpsUrl instance; public HttpsUrl(){ } public static HttpsUrl getInstance(){ if(null == instance){ synchronized (HttpsUrl.class){ if(instance==null){ instance = new HttpsUrl(); } } } return instance; } public void get(String url, Map<String , String> map, final CallBack callback, final Class cls){ if(TextUtils.isEmpty(url)){ return; } StringBuffer sb = new StringBuffer(); sb.append(url); if(url.contains("?")){ if (url.indexOf("?")==url.length()-1) { }else{ sb.append("&"); } }else{ sb.append("?"); } for(Map.Entry<String ,String > entry:map.entrySet()){ sb.append(entry.getKey()) .append("=") .append(entry.getValue()) .append("&"); } if(sb.indexOf("&") != -1){ sb.deleteCharAt(sb.lastIndexOf("&")); } OkHttpClient client = new OkHttpClient(); final Request request = new Request.Builder() .get() .url(sb.toString()) .build(); Call call = client.newCall(request); call.enqueue(new okhttp3.Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { callback.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String s = response.body().string(); Log.i("ddd",s); handler.post(new Runnable() { private Object o; @Override public void run() { if(TextUtils.isEmpty(s)){ o = null; }else{ Gson gson = new Gson(); o = gson.fromJson(s,cls); } callback.onResponse(o); } }); } }); } }
CallBack
package com.example.carlication.Call; /**
package com.example.carlication.Call; /** * Created by lenovo on 2017/12/15. */ public interface IAddCartView { void show(String code); }
IAddCartView
package com.example.carlication.Call; /** * Created by lenovo on 2017/12/15. */ public interface IAddCartView { void show(String code); }
ICartView
package com.example.carlication.Call; import com.example.carlication.Bean.GetCartBean; import java.util.List; /** * Created by lenovo on 2017/12/15. */ public interface ICartView { void show(List<GetCartBean.DataBean> group, List<List<GetCartBean.DataBean.ListBean>> child); }
INewsView
package com.example.carlication.Call; import com.example.carlication.Bean.MoreBean; /** * Created by lenovo on 2017/12/15. */ public interface INewsView { void failed(Exception e); void success(MoreBean.DataBean data); }
NewsPersenter
package com.example.carlication.Persenter; import android.util.Log; import com.example.carlication.Bean.MoreBean; import com.example.carlication.Call.CallBack; import com.example.carlication.Call.INewsView; import com.example.carlication.Http.HttpsUrl; import java.util.HashMap; /** * Created by lenovo on 2017/12/15. */ public class NewsPersenter { private INewsView inv; public NewsPersenter(INewsView inv) { this.inv = inv; } public void getNews(){ HashMap<String , String> map = new HashMap<>(); map.put("source","android"); map.put("pid","71"); HttpsUrl.getInstance().get("https://www.zhaoapi.cn/product/getProductDetail", map, new CallBack() { @Override public void onResponse(Object o) { MoreBean json = (MoreBean) o; if(json!=null){ MoreBean.DataBean data = json.getData(); Log.i("jjj",data.toString()); inv.success(data); } } @Override public void onFailure(Exception e) { inv.failed(e); } },MoreBean.class); } public void detachView(){ if (inv != null) { inv = null; } } }
GetCartPersenter
package com.example.carlication.Persenter; import com.example.carlication.Bean.GetCartBean; import com.example.carlication.Call.CallBack; import com.example.carlication.Call.ICartView; import com.example.carlication.Http.HttpsUrl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Created by lenovo on 2017/12/15. */ public class GetCartPersenter { private ICartView icv; public GetCartPersenter(ICartView icv) { this.icv = icv; } public void getNews(){ HashMap<String , String> map = new HashMap<>(); map.put("source","android"); map.put("uid","101"); map.put("pid","71"); HttpsUrl.getInstance().get("http://120.27.23.105/product/getCarts", map, new CallBack() { @Override public void onResponse(Object o) { GetCartBean cartBean = (GetCartBean) o; if(cartBean!=null){ List<GetCartBean.DataBean> group = cartBean.getData(); List<List<GetCartBean.DataBean.ListBean>> child = new ArrayList<>(); for (int i = 0; i < group.size(); i++){ child.add(group.get(i).getList()); } icv.show(group,child); } } @Override public void onFailure(Exception e) { } },GetCartBean.class); } }
GetCartBean
package com.example.carlication.Bean; import java.util.List; /** * Created by lenovo on 2017/12/15. */ public class GetCartBean { /** * msg : 请求成功 * code : 0 * data : [{"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"}] */ private String msg; private String code; /** * 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 */ 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 { private boolean checked;//自己添加的 public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } private String sellerName; private String sellerid; /** * bargainPrice : 3455.0 * 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.0 * pscid : 39 * selected : 0 * sellerid : 6 * subhead : 【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机 * title : 小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】 */ 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 { private boolean checked;//自己添加的 private int count = 1;//自己添加的 public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } 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; } } } }
DataBean
package com.example.carlication.Bean; /** * Created by lenovo on 2017/12/15. */ public class MoreBean { /** * msg : * seller : {"description":"我是商家4","icon":"http://120.27.23.105/images/icon.png","name":"商家4","productNums":999,"score":5,"sellerid":4} * code : 0 * data : {"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","itemtype":2,"pid":11,"price":8989,"pscid":1,"salenum":0,"sellerid":4,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"} */ private String msg; /** * description : 我是商家4 * icon : http://120.27.23.105/images/icon.png * name : 商家4 * productNums : 999 * score : 5.0 * sellerid : 4 */ private SellerBean seller; private String code; /** * 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 * itemtype : 2 * pid : 11 * price : 8989.0 * pscid : 1 * salenum : 0 * sellerid : 4 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public SellerBean getSeller() { return seller; } public void setSeller(SellerBean seller) { this.seller = seller; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class SellerBean { private String description; private String icon; private String name; private int productNums; private double score; private int sellerid; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getProductNums() { return productNums; } public void setProductNums(int productNums) { this.productNums = productNums; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } } public static class DataBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; 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 getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } 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 getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } 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; } } }
购物车的main布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.carlication.SecondActivity"> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/cb" android:text="合计:" /> <TextView android:id="@+id/tvCount" android:layout_width="100dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="#ff0000" android:gravity="center" android:text="去结算(0)" android:textColor="#ffffff" /> </RelativeLayout> </LinearLayout >
SecondActivity
package com.example.carlication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import com.example.carlication.Adapter.ElvAdapter; import com.example.carlication.Bean.GetCartBean; import com.example.carlication.Call.ICartView; import com.example.carlication.Persenter.GetCartPersenter; import java.util.List; public class SecondActivity extends AppCompatActivity implements ICartView { private ExpandableListView mElv; /** * 全选 */ private CheckBox mCb; /** * 合计: */ private TextView mTvTotal; /** * 去结算(0) */ private TextView mTvCount; private GetCartPersenter presenter; private ElvAdapter elvAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); presenter = new GetCartPersenter(this); presenter.getNews(); mCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { elvAdapter.AllOrNone(mCb.isChecked()); } }); } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCb = (CheckBox) findViewById(R.id.cb); mTvTotal = (TextView) findViewById(R.id.tvTotal); mTvCount = (TextView) findViewById(R.id.tvCount); } @Override public void show(List<GetCartBean.DataBean> group, List<List<GetCartBean.DataBean.ListBean>> child) { elvAdapter = new ElvAdapter(this,group,child); mElv.setGroupIndicator(null); mElv.setAdapter(elvAdapter); for (int i = 0; i < group.size(); i++) { mElv.expandGroup(i); } } public void setPriceAndCount(PriceAndCount priceAndCount) { mTvTotal.setText("合计:" + priceAndCount.getPrice()); mTvCount.setText("去结算(" + priceAndCount.getCount() + ")"); } public void setAllChecked(boolean bool) { mCb.setChecked(bool); } }
ElvAdapter
package com.example.carlication.Adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.carlication.Bean.GetCartBean; import com.example.carlication.PriceAndCount; import com.example.carlication.R; import com.example.carlication.SecondActivity; import java.util.List; /** * Created by lenovo on 2017/12/15. */ public class ElvAdapter extends BaseExpandableListAdapter { private final LayoutInflater inflater; private Context context; private List<GetCartBean.DataBean> group; private List<List<GetCartBean.DataBean.ListBean>> child; public ElvAdapter(Context context, List<GetCartBean.DataBean> group, List<List<GetCartBean.DataBean.ListBean>> child) { this.context=context; this.group=group; this.child=child; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return group.size(); } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; final GroupViewHolder holder; if (convertView == null) { view = inflater.inflate(R.layout.elv_group, null); holder = new GroupViewHolder(); holder.tv = view.findViewById(R.id.tvGroup); holder.cbGroup = view.findViewById(R.id.cbGroup); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } final GetCartBean.DataBean dataBean = group.get(groupPosition); holder.tv.setText(dataBean.getSellerName()); holder.cbGroup.setChecked(dataBean.isChecked()); holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.一级列表的checkbox状态值 dataBean.setChecked(holder.cbGroup.isChecked()); //2.二级列表的checkbox状态值 setChildrenCb(groupPosition, holder.cbGroup.isChecked()); //3.全选的checkbox状态值 ((SecondActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); return view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, final View convertView, ViewGroup parent) { View view; final ChildViewHolder holder; if (convertView == null) { view = inflater.inflate(R.layout.elv_child, null); holder = new ChildViewHolder(); holder.iv = view.findViewById(R.id.iv); holder.tvTitle = view.findViewById(R.id.tvTitle); holder.tvSubhead = view.findViewById(R.id.tvSubhead); holder.tvPrice = view.findViewById(R.id.tvPrice); holder.cbChild = view.findViewById(R.id.cbChild); holder.btDel = view.findViewById(R.id.btDel); holder.tvNum = view.findViewById(R.id.tvNum); holder.ivDel = view.findViewById(R.id.ivDel); holder.ivAdd = view.findViewById(R.id.ivAdd); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } final GetCartBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition); String images = listBean.getImages(); Glide.with(context).load(images.split("\\|")[0]).into(holder.iv); holder.tvTitle.setText(listBean.getTitle()); holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked()); holder.tvSubhead.setText(listBean.getSubhead()); holder.tvPrice.setText(listBean.getPrice() + "元"); holder.tvNum.setText(listBean.getCount() + ""); //给checkbox设置点击事件 holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.二级列表的checkbox状态值 listBean.setChecked(holder.cbChild.isChecked()); //2.一级列表的checkbox状态值 group.get(groupPosition).setChecked(isAllChildCbChecked(groupPosition)); //3.全选的checkbox状态值 ((SecondActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); holder.ivAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); count++; //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); holder.ivDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); if (count <= 1) { count = 1; } else { count--; } //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); holder.btDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //其实就是删除集合 List<GetCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); if (listBeans.size() > 0) { listBeans.remove(childPosition); } if (listBeans.size() == 0) { child.remove(groupPosition); group.remove(groupPosition); } //计算钱和数量并显示 setPriceAndCount(); //改变全选状态 ((SecondActivity) context).setAllChecked(isAllGroupCbChecked()); //刷新列表 notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { TextView tv; CheckBox cbGroup; } class ChildViewHolder { ImageView iv; TextView tvTitle; TextView tvSubhead; TextView tvPrice; CheckBox cbChild; Button btDel; TextView tvNum; ImageView ivDel; ImageView ivAdd; } /** * 设置一级列表对应的二级列表checkbox状态 * * @param groupPosition * @param bool */ private void setChildrenCb(int groupPosition, boolean bool) { List<GetCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { listBeans.get(i).setChecked(bool); } } /** * 判断一级列表checkbox状态 * * @return */ private boolean isAllGroupCbChecked() { if (group.size() == 0) { return false; } for (int i = 0; i < group.size(); i++) { if (!group.get(i).isChecked()) { return false; } } return true; } /** * 判断二级列表checkbox状态 * * @return */ private boolean isAllChildCbChecked(int groupPosition) { List<GetCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { if (!listBeans.get(i).isChecked()) { return false; } } return true; } /** * 设置钱和数量 */ private void setPriceAndCount() { ((SecondActivity) context).setPriceAndCount(compute()); } /** * 计算钱和数量 */ private PriceAndCount compute() { double price = 0; int count = 0; for (int i = 0; i < group.size(); i++) { List<GetCartBean.DataBean.ListBean> listBeans = child.get(i); for (int j = 0; j < listBeans.size(); j++) { if (listBeans.get(j).isChecked()) { price += listBeans.get(j).getPrice() * listBeans.get(j).getCount(); count += listBeans.get(j).getCount(); } } } return new PriceAndCount(price, count); } /** * 全选或者全不选 * * @param bool */ public void AllOrNone(boolean bool) { for (int i = 0; i < group.size(); i++) { group.get(i).setChecked(bool); setChildrenCb(i, bool); } setPriceAndCount(); notifyDataSetChanged(); } }
elv_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:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cbGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvGroup" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" /> </LinearLayout>
elv_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="120dp" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="50dp"> <CheckBox android:id="@+id/cbChild" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvSubhead" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tvPrice" android:textColor="#F00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_marginLeft="10dp" android:id="@+id/ivDel" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_del" /> <TextView android:id="@+id/tvNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:text="1"/> <ImageView android:id="@+id/ivAdd" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_add" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/btDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> </LinearLayout>