设置点击进入购物车之后的代码
首先添加依赖
compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'org.greenrobot:eventbus:3.1.1'
//添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.CAMERA" />
Main2Activity布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/top_bar" android:layout_width="match_parent" android:layout_height="48dp" android:background="#f7f7f7" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="@android:color/transparent" android:orientation="vertical" > <ImageView android:id="@+id/back" android:layout_width="48dp" android:layout_height="48dp" android:layout_alignParentLeft="true" android:layout_gravity="center_vertical" android:padding="12dp" android:src="@drawable/back" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:minHeight="48dp" android:text="购物车" android:textColor="#1a1a1a" android:textSize="16sp" /> <TextView android:id="@+id/edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="40dp" android:gravity="center" android:minHeight="48dp" android:text="编辑" android:textColor="#1a1a1a" android:textSize="14sp" android:visibility="visible" /> </RelativeLayout> </LinearLayout> <ExpandableListView android:id="@+id/exListView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:childIndicator="@null" android:groupIndicator="@null" > </ExpandableListView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal" > <CheckBox android:id="@+id/all_chekbox" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选"/> <LinearLayout android:id="@+id/ll_info" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginRight="20dp" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="right" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="合计:" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/total_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥0.00" android:textColor="#f23232" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <TextView android:id="@+id/total_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="共有商品:0件" android:gravity="right" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <TextView android:id="@+id/tv_go_to_pay" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="#fd7a05" android:clickable="true" android:gravity="center" android:text="结算" android:textColor="#FAFAFA" /> </LinearLayout> </LinearLayout> </LinearLayout>
edit.xml设置在drawable文件夹下 用来设置EditText背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <!--<corners android:radius="3dip"/>--> <stroke android:width="1dip" android:color="#BDC7D8" /> </shape>values下的attrs.xml方法
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="AddDeleteViewStyle"> <attr name="left_text" format="string"></attr> <attr name="right_text" format="string"></attr> <attr name="middle_text" format="string"></attr> <attr name="left_text_color" format="color"></attr> </declare-styleable> </resources>
//一级列表布局
<?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" xmlns:app="http://schemas.android.com/apk/res-auto"> <CheckBox android:id="@+id/child_checkbox" android:layout_marginTop="50dp" android:layout_marginLeft="20dp" android:layout_marginBottom="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginLeft="20dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/shop_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="17dp" android:layout_marginStart="17dp" android:text="TextView" android:layout_alignParentTop="true" /> <ImageView android:id="@+id/shop_img" android:layout_width="90dp" android:layout_height="90dp" android:layout_marginTop="30dp" app:srcCompat="@mipmap/ic_launcher" android:layout_below="@+id/shop_name" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/shop_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/shop_img" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="¥20" android:textColor="#f23232"/> <wuhao.bwei.com.wuhaoshopunderstand.AddDeleteView android:id="@+id/adv" android:layout_width="160dp" android:layout_height="30dp" android:layout_below="@+id/shop_price" android:layout_marginTop="30dp" android:layout_marginLeft="140dp" app:left_text="-" app:right_text="+" app:middle_text="1" android:focusable="false" > </wuhao.bwei.com.wuhaoshopunderstand.AddDeleteView> <Button android:id="@+id/shop_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:visibility="invisible" android:text="删除" /> </RelativeLayout> </LinearLayout>
二级列表布局
<?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"> <CheckBox android:id="@+id/group_checkbox" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false"/> <TextView android:id="@+id/shop_name" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" /> </LinearLayout>
加减器
<?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" android:weightSum="1"> <TextView android:id="@+id/txt_delete" android:layout_width="30dp" android:layout_height="30dp" android:text="减" android:gravity="center" android:background="#8b948b"/> <EditText android:id="@+id/et_number" android:layout_marginTop="2dp" android:layout_width="50dp" android:layout_height="30dp" android:background="@drawable/edit" android:layout_weight="0.00" android:gravity="center" android:text="1"/> <TextView android:id="@+id/txt_add" android:layout_width="30dp" android:layout_height="30dp" android:text="加" android:gravity="center" android:background="#8b948b"/> </LinearLayout>
AddDeleteView设置自定义view
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; /** * Created by alienware on 2017/12/18. */ public class AddDeleteView extends LinearLayout{ private OnAddDelClickListener listener; private EditText etNumber; //对外提供一个点击的回调接口 public interface OnAddDelClickListener{ void onAddClick(View v); void onDelClick(View v); } public void setOnAddDelClickListener(OnAddDelClickListener listener){ if(listener!=null){ this.listener=listener; } } public AddDeleteView(Context context) { this(context,null); } public AddDeleteView(Context context, AttributeSet attrs) { this(context, attrs,0); } public AddDeleteView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private void initView(Context context, AttributeSet attrs, int defStyleAttr) { View.inflate(context,R.layout.layout_add_delete,this); //获取控件 TextView txtDelete=findViewById(R.id.txt_delete); TextView txtAdd=findViewById(R.id.txt_add); etNumber = findViewById(R.id.et_number); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteViewStyle); String leftText = typedArray.getString(R.styleable.AddDeleteViewStyle_left_text); String rightText = typedArray.getString(R.styleable.AddDeleteViewStyle_right_text); String middleText = typedArray.getString(R.styleable.AddDeleteViewStyle_middle_text); int color = typedArray.getColor(R.styleable.AddDeleteViewStyle_left_text_color, Color.RED); txtDelete.setText(leftText); txtAdd.setText(rightText); etNumber.setText(middleText); txtDelete.setTextColor(color); //回收 typedArray.recycle(); txtDelete.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listener.onDelClick(view); } }); txtAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listener.onAddClick(view); } }); } //对外提供一个修改数字的方法 public void setNumber(int number){ if(number>0){ etNumber.setText(number+""); } } //对外提供一个获取当前数字的方法 public int getNumber(){ String string = etNumber.getText().toString(); int i = Integer.parseInt(string); return i; } }
utils包下的GsonUtils
public class GsonUtils { private static Gson gson; public static Gson getInstance(){ if(gson==null){ gson = new Gson(); } return gson; } }
View层定义两个接口
1.CallBack
public interface CallBack { void onSuccess(String tag,Object o); void onFailed(String tag,Exception e); }2.INewView
public interface INewView { //创建接口 void success(String tag,Object o); void failed(String tag,Exception e); }model层
public class HttpUtils { private static final String TAG = "HttpUtils"; private static HttpUtils instance; Handler handler = new Handler(); public HttpUtils(){ } public static HttpUtils getInstance(){ if(null==instance){ synchronized (HttpUtils.class){ if(instance==null){ instance = new HttpUtils(); } } } return instance; } //重写get请求 public void get(String url, Map<String,String> map, final CallBack callback, final Class cla, final String tag){ if(TextUtils.isEmpty(url)){ return; } StringBuffer sb = new StringBuffer(); sb.append(url); if(!map.isEmpty()){ Log.i(TAG, "get: 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("&")); } } Log.i(TAG, "get: url="+sb); OkHttpClient client = new OkHttpClient.Builder().build(); final Request request = new Request.Builder() .get() .url(sb.toString()) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { //失败 handler.post(new Runnable() { @Override public void run() { callback.onFailed(tag,e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String result = response.body().string(); Log.e(TAG, "onResponse: result--"+result); handler.post(new Runnable() { @Override public void run() { Object o; if (TextUtils.isEmpty(result)) { o = null; } else { o = GsonUtils.getInstance().fromJson(result, cla); } //Log.e(TAG, "onResponse: bean--"+); callback.onSuccess(tag,o); } }); } }); } }
P层
public class NewsPresenter { private static final String TAG = "NewsPresenter"; private INewView inv; public void attachView(INewView inv){ this.inv=inv; } public void get(String url, Map<String, String> map, String tag, Class cla){ HttpUtils.getInstance().get(url, map, new CallBack() { @Override public void onSuccess(String tag, Object o) { if(o!=null){ inv.success(tag,o); //Log.i(TAG, "onSuccess:++++++++++++++++++++++++ "+gridSudokuBeanData.toString()); } } @Override public void onFailed(String tag, Exception e) { inv.failed(tag,e); } }, cla,tag); } public void deleteView(){ if(inv!=null){ inv=null; } } }bean包下的类
public class ChildBean { private String title; private String images; private double price; private int num; private boolean childCb; private boolean btn; public ChildBean(String title, String images, double price, int num, boolean childCb, boolean btn) { this.title = title; this.images = images; this.price = price; this.num = num; this.childCb = childCb; this.btn = btn; } public boolean isBtn() { return btn; } public void setBtn(boolean btn) { this.btn = btn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public boolean isChildCb() { return childCb; } public void setChildCb(boolean childCb) { this.childCb = childCb; } @Override public String toString() { return "ChildBean{" + "title='" + title + '\'' + ", images='" + images + '\'' + ", price=" + price + ", num=" + num + ", childCb=" + childCb + '}'; } }
2.
public class GroupBean { private String sellerName; private boolean gropuCb; public GroupBean(String sellerName, boolean gropuCb) { this.sellerName = sellerName; this.gropuCb = gropuCb; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public boolean isGropuCb() { return gropuCb; } public void setGropuCb(boolean gropuCb) { this.gropuCb = gropuCb; } @Override public String toString() { return "GroupBean{" + "sellerName='" + sellerName + '\'' + ", gropuCb=" + gropuCb + '}'; } }3.
public class ShopCarBean { /** * code : 0 * data : [{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","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":84,"pid":10,"price":555.55,"pscid":1,"selected":0,"sellerid":3,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":61,"price":14999,"pscid":40,"selected":0,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":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":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"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":69,"price":16999,"pscid":40,"selected":0,"sellerid":13,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家13","sellerid":"13"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","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":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","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":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"}] * msg : 请求成功 */ private String code; private String msg; private List<DataBean> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * list : [{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}] * sellerName : 商家1 * sellerid : 1 */ 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 : 22.9 * createtime : 2017-10-14T21:48:08 * detailUrl : https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg * num : 2 * pid : 24 * price : 288.0 * pscid : 2 * selected : 0 * sellerid : 1 * subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》 * title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋 */ 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; } } } }
购物车的adapter
public class ExpandableAdapter extends BaseExpandableListAdapter{ // private static final String TAG = "ExpandableAdapter二级列表适配器"; private Context context; private List<GroupBean> groupBeen=new ArrayList<>(); private List<List<ChildBean>> childBeen=new ArrayList<>(); public ExpandableAdapter(Context context, List<GroupBean> groupBeen, List<List<ChildBean>> childBeen) { this.context = context; this.groupBeen = groupBeen; this.childBeen = childBeen; } @Override public int getGroupCount() { return groupBeen.size(); } @Override public int getChildrenCount(int i) { return childBeen.get(i).size(); } @Override public Object getGroup(int i) { return groupBeen.get(i).getSellerName(); } @Override public Object getChild(int i, int i1) { return childBeen.get(i).get(i1).getTitle(); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } //一级组 @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { //加载视图 view=View.inflate(context, R.layout.ex_group_item ,null); final CheckBox groupCb= view.findViewById(R.id.group_checkbox); TextView shopName= view.findViewById(R.id.shop_name); shopName.setText(groupBeen.get(i).getSellerName()); groupCb.setChecked(groupBeen.get(i).isGropuCb()); //组复选按钮 groupCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean gchecked = groupCb.isChecked(); groupBeen.get(i).setGropuCb(gchecked); Main2Activity main= (Main2Activity) context; for(GroupBean i: groupBeen){ boolean gropuCb = i.isGropuCb(); if(!gropuCb){ main.allCheckbox.setChecked(false); break; }else{ main.allCheckbox.setChecked(true); } } int size = childBeen.get(i).size(); if(gchecked){ for(int r=0;r<size;r++){ //Toast.makeText(context,"group按钮"+ gchecked+""+size, Toast.LENGTH_SHORT).show(); childBeen.get(i).get(r).setChildCb(true); } }else{ for(int r=0;r<size;r++){ //Toast.makeText(context,"group按钮"+ gchecked+""+size, Toast.LENGTH_SHORT).show(); childBeen.get(i).get(r).setChildCb(false); } } notifyDataSetChanged(); main.changesum(childBeen); } }); return view; } //二级组 @Override public View getChildView(final int i, final int i1, boolean b, View v, ViewGroup viewGroup) { //加载视图 v=View.inflate(context, R.layout.ex_child_item ,null); final CheckBox childCb = v.findViewById(R.id.child_checkbox); TextView shopTitle= v.findViewById(R.id.shop_title); TextView shopPrice= v.findViewById(R.id.shop_price); ImageView shopImg=v.findViewById(R.id.shop_img); final AddDeleteView adv = v.findViewById(R.id.adv); Button shop_delete=v.findViewById(R.id.shop_delete); childCb.setChecked(childBeen.get(i).get(i1).isChildCb()); Glide.with(context).load(childBeen.get(i).get(i1).getImages()).into(shopImg); shopTitle.setText(childBeen.get(i).get(i1).getTitle()); shopPrice.setText(childBeen.get(i).get(i1).getPrice()+""); adv.setNumber(childBeen.get(i).get(i1).getNum()); final Main2Activity main= (Main2Activity) context; //控制删除按钮的显隐 if(childBeen.get(i).get(i1).isBtn()){ shop_delete.setVisibility(View.VISIBLE); }else{ shop_delete.setVisibility(View.INVISIBLE); } //删除按钮监听 shop_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int size = childBeen.get(i).size(); if(size==1){ childBeen.remove(i); groupBeen.remove(i); }else{ childBeen.get(i).remove(i1); } //点击删除后隐藏所有删除按钮 for (List<ChildBean> i1:childBeen){ for(int r=0;r<i1.size();r++) { i1.get(r).setBtn(false); } } notifyDataSetChanged(); main.changesum(childBeen); } }); //加减器逻辑 adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() { @Override public void onAddClick(View v) { int number = adv.getNumber(); number++; adv.setNumber(number); childBeen.get(i).get(i1).setNum(number); main.changesum(childBeen); } @Override public void onDelClick(View v) { int number = adv.getNumber(); number--; adv.setNumber(number); childBeen.get(i).get(i1).setNum(number); main.changesum(childBeen); } }); //二级组的复选框监听 childCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean flag=false; boolean cchecked = childCb.isChecked(); childBeen.get(i).get(i1).setChildCb(cchecked); //Toast.makeText(context,"child按钮"+ cchecked+""+i1, Toast.LENGTH_SHORT).show(); Main2Activity main= (Main2Activity) context; for (List<ChildBean> i1:childBeen){ for(int r=0;r<i1.size();r++) { boolean childCb1 = i1.get(r).isChildCb(); if(!childCb1){ main.allCheckbox.setChecked(false); groupBeen.get(i).setGropuCb(false); flag=true; break; }else{ main.allCheckbox.setChecked(true); groupBeen.get(i).setGropuCb(true); } } if(flag){ break; } } int size = childBeen.get(i).size(); for(int x=0;x<size;x++) { boolean childCb1 = childBeen.get(i).get(x).isChildCb(); if(!childCb1){ groupBeen.get(i).setGropuCb(false); break; }else{ groupBeen.get(i).setGropuCb(true); } } notifyDataSetChanged(); main.changesum(childBeen); } }); return v; } @Override public boolean isChildSelectable(int i, int i1) { return false; } }
主函数的方法
public class Main2Activity extends AppCompatActivity implements INewView{ //定义全局变量 private ExpandableListView exListView; public CheckBox allCheckbox; private TextView totalPrice; private TextView totalnumber; List<GroupBean> groupBeen=new ArrayList<>(); List<List<ChildBean>> childBeen=new ArrayList<>(); private NewsPresenter presenter; private ExpandableAdapter expandableAdapter; private TextView edit; private boolean flagedit=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); getSupportActionBar().hide(); initView(); presenter = new NewsPresenter(); presenter.attachView(this); Map<String,String> map = new HashMap<>(); presenter.get("http://120.27.23.105/product/getCarts?uid=100",map,"car",ShopCarBean.class); //获取二级列表适配器 expandableAdapter = new ExpandableAdapter(Main2Activity.this, groupBeen, childBeen); exListView.setAdapter(expandableAdapter); for(int i = 0; i < expandableAdapter.getGroupCount(); i++){ exListView.expandGroup(i); } exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { return true; } }); //全选监听 allCheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = allCheckbox.isChecked(); //改变一级item复选框 for (GroupBean i:groupBeen){ i.setGropuCb(checked); } //改变二级item复选框 for (List<ChildBean> i1:childBeen){ for(int r=0;r<i1.size();r++) { i1.get(r).setChildCb(checked); } } expandableAdapter.notifyDataSetChanged(); changesum(childBeen); //Toast.makeText(Main2Activity.this,"全选按钮"+checked,Toast.LENGTH_SHORT).show(); } }); edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (List<ChildBean> i1:childBeen){ for(int r=0;r<i1.size();r++) { i1.get(r).setBtn(flagedit); } } flagedit=!flagedit; expandableAdapter.notifyDataSetChanged(); } }); } private void initView() { exListView = (ExpandableListView) findViewById(R.id.exListView); allCheckbox = (CheckBox)findViewById(R.id.all_chekbox); totalPrice = (TextView)findViewById(R.id.total_price); totalnumber = (TextView)findViewById(R.id.total_number); edit = (TextView) findViewById(R.id.edit); } @Override public void success(String tag, Object o) { //成功的方法 if(o!=null&&tag.equals("car")){ ShopCarBean shopCarBean = (ShopCarBean) o; List<ShopCarBean.DataBean> data = shopCarBean.getData(); for(ShopCarBean.DataBean i:data){ GroupBean groupBean = new GroupBean(i.getSellerName(),false); this.groupBeen.add(groupBean); List<ShopCarBean.DataBean.ListBean> list = i.getList(); List<ChildBean> ls = new ArrayList<>(); for(ShopCarBean.DataBean.ListBean w:list){ String[] split = w.getImages().split("\\|"); ChildBean childBean = new ChildBean(w.getTitle(),split[0],w.getPrice(),1,false,false); ls.add(childBean); } this.childBeen.add(ls); } for(int i = 0; i < expandableAdapter.getGroupCount(); i++){ exListView.expandGroup(i); } expandableAdapter.notifyDataSetChanged(); } } @Override public void failed(String tag, Exception e) { Toast.makeText(Main2Activity.this,e.getMessage()+"",Toast.LENGTH_SHORT).show(); } //计算和数量总价 public void changesum(List<List<ChildBean>> childBeen){ //冒泡排序 int count=0; double sum=0; for (List<ChildBean> i1:childBeen){ for(int r=0;r<i1.size();r++) { boolean childCb1 = i1.get(r).isChildCb(); if(childCb1){ double price = i1.get(r).getPrice(); int num = i1.get(r).getNum(); sum+=price*num; count++; } } } totalPrice.setText("¥"+sum); totalnumber.setText("共有商品:"+count+"件"); } }