布局
Frag
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:textSize="20dp" android:text="购物车" /> <TextView android:id="@+id/tv_bianji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textSize="20dp" android:text="编辑" /> </RelativeLayout> <ExpandableListView android:id="@+id/exlist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> </ExpandableListView> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" android:padding="10dp"> <CheckBox android:id="@+id/check_all" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="全选" android:textSize="15dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:text="总价:" android:textSize="15dp" /> <TextView android:id="@+id/tv_zjprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:text="0" android:textSize="15dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:text="数量:" android:textSize="15dp" /> <TextView android:id="@+id/tv_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:gravity="center_vertical" android:text="0" android:textSize="15dp" /> <Button android:id="@+id/btn_js" android:layout_width="80dp" android:layout_height="40dp" android:layout_gravity="right" android:background="#fc0109" android:text="结算" android:textSize="15dp" /> </LinearLayout> </LinearLayout>
cartli01
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/shang" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="15dp" /> </LinearLayout>
cartlistview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/cartlv" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
cartchidlist
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/cartCk" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/carttu" android:layout_width="100dp" android:layout_height="100dp" android:layout_toRightOf="@+id/cartCk" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_toRightOf="@+id/carttu" android:layout_height="wrap_content"> <TextView android:id="@+id/cartname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="999" android:textSize="15dp" /> <TextView android:id="@+id/cartprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="999" android:textColor="#f00" /> </LinearLayout> </RelativeLayout>
bean包ModelBean
public class ModelBean { /** * msg : 请求成功 * code : 0 * data : [{"list":[],"sellerName":"","sellerid":"0"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":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":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"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","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":4,"pid":56,"price":99,"pscid":39,"selected":1,"sellerid":12,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家12","sellerid":"12"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","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":1,"pid":40,"price":345,"pscid":2,"selected":1,"sellerid":17,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家17","sellerid":"17"},{"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":78,"price":999,"pscid":40,"selected":1,"sellerid":22,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家22","sellerid":"22"}] */ private String msg; private String code; private List<DataBean> data; /** * list : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}] * sellerName : 商家1 * sellerid : 1 */ private String sellerName; private String sellerid; private List<ListBean> list; 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 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 DataBean { /** * list : [] * sellerName : * sellerid : 0 */ private String sellerName; private String sellerid; private List<ListBean> list; public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } } public static class ListBean { /** * bargainPrice : 11800 * createtime : 2017-10-10T17:33:37 * detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg * num : 2 * pid : 57 * price : 5199 * pscid : 40 * selected : 0 * sellerid : 1 * subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统 * title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银 */ private String bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private String price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } 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 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 String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSelected() { return selected; } public void setSelected(int selected) { this.selected = selected; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
model包ShopCartModel
public class ShopCartModel { public void ShopCartDemo(String uid,final OnPostCallBack5 onPostCallBack5){ OkHttpClient okHttpClient=new OkHttpClient.Builder().build(); FormBody formBody=new FormBody.Builder() .add("uid",uid) .add("source","android") .build(); Request request=new Request.Builder().url("https://www.zhaoapi.cn/product/getCarts").post(formBody).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { if(onPostCallBack5!=null){ onPostCallBack5.OnPostFail(e); } } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); ModelBean modelBean=new Gson().fromJson(string,ModelBean.class); if(onPostCallBack5!=null){ onPostCallBack5.OnPostSuccese(modelBean); Log.d("啊啊啊啊啊啊啊啊啊啊啊啊",modelBean.toString()); } } }); } public interface OnPostCallBack5{ void OnPostSuccese(ModelBean modelBean); void OnPostFail(Exception e); } }
presenter包ShopCartPresenter
public class ShopCartPresenter { private ShopCartModel shopCartModel; public void ShopCartDemo(String uid, final IviewCart iviewCart){ shopCartModel=new ShopCartModel(); shopCartModel.ShopCartDemo(uid, new ShopCartModel.OnPostCallBack5() { @Override public void OnPostSuccese(ModelBean modelBean) { if(iviewCart!=null){ iviewCart.OnSuccese(modelBean); Log.d("nnnnnnnnnnnnnnnnnn",modelBean.getMsg()); } } @Override public void OnPostFail(Exception e) { if(iviewCart!=null){ iviewCart.OnFail(e); } } }); } }
view包IviewCart
public interface IviewCart { void OnSuccese(ModelBean modelBean); void OnFail(Exception e); }
Frag
public class Frag3 extends Fragment implements View.OnClickListener{ private ShopCartPresenter shopCartPresenter; private String uid="72"; private ExpandableListView exlist; private List<ModelBean.DataBean> data; private ListView cartlv; private List<ModelBean.ListBean> list; private MyAdapter6 myAdapter6; private CheckBox check_all; private Double jia=0.0; private TextView tv_zjprice; private Double zongjia; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.frag3,null); exlist= (ExpandableListView) view.findViewById(R.id.exlist); check_all= (CheckBox) view.findViewById(R.id.check_all); tv_zjprice= (TextView) view.findViewById(R.id.tv_zjprice); check_all.setOnClickListener(this); //默认展开 int count = exlist.getCount(); for (int i=0; i<count; i++) { exlist.expandGroup(i); }; shopCartPresenter=new ShopCartPresenter(); shopCartPresenter.ShopCartDemo(uid, new IviewCart() { @Override public void OnSuccese(final ModelBean modelBean) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { // Log.d("或许就能参加上半场时间成本",data.toString()); data = modelBean.getData(); myAdapter6=new MyAdapter6(); exlist.setAdapter(myAdapter6); } }); } @Override public void OnFail(Exception e) { } }); return view; } @Override public void onClick(View v) { for (int i = 0; i <data.size() ; i++) { List<ModelBean.ListBean> list = data.get(i).getList(); for (int j = 0; j <list.size() ; j++) { // boolean flag = list.get(j).isFlag(); list.get(j).setFlag(!list.get(j).isFlag()); } } myAdapter6.notifyDataSetChanged(); } class MyAdapter6 extends BaseExpandableListAdapter { @Override public int getGroupCount() { return data.size(); } @Override public int getChildrenCount(int groupPosition) { return data.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return data.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return data.get(groupPosition).getList().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 true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView=convertView.inflate(getActivity(),R.layout.cartli01,null); TextView shangjia= (TextView) convertView.findViewById(R.id.shang); shangjia.setText(data.get(groupPosition).getSellerName()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { convertView=convertView.inflate(getActivity(),R.layout.cartlistview,null); cartlv= (ListView) convertView.findViewById(R.id.cartlv); list = data.get(groupPosition).getList(); MyAdapter7 myAdapter7=new MyAdapter7(); cartlv.setAdapter(myAdapter7); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //总价 public Double zongjia(List<ModelBean.DataBean> data){ // List<ModelBean.DataBean> data = modelBean.getData(); for (int i=0;i<data.size();i++){ List<ModelBean.ListBean> list = data.get(i).getList(); for(int j=0;j<list.size();j++){ boolean flag = list.get(j).isFlag(); if(flag=true){ String price = list.get(j).getPrice(); Double jiage = Double.valueOf(price); jia+=jiage; } } } tv_zjprice.setText(zongjia+""); return jia; } class MyAdapter7 extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null){ holder=new ViewHolder(); convertView=convertView.inflate(getActivity(),R.layout.cartchidlist,null); holder.cartname= (TextView) convertView.findViewById(R.id.cartname); holder.cartprice= (TextView) convertView.findViewById(R.id.cartprice); holder.carttu= (ImageView) convertView.findViewById(R.id.carttu); holder.cartCk= (CheckBox) convertView.findViewById(R.id.cartCk); holder.cartCk.setChecked(list.get(position).isFlag()); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } holder.cartname.setText(list.get(position).getTitle()); holder.cartprice.setText(list.get(position).getPrice()); String images = list.get(position).getImages(); String[] split = images.split("\\|"); Glide.with(getActivity()).load(split[0]).into(holder.carttu); holder.cartCk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean flag = list.get(position).isFlag(); flag=!flag; list.get(position).setFlag(flag); zongjia = zongjia(data); notifyDataSetChanged(); } }); return convertView; } class ViewHolder{ ImageView carttu; TextView cartname,cartprice; CheckBox cartCk; } } } }