二级列表 购物车
2017-11-21 12:05
411 查看
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.liaoinstan.springview:library:1.2.6' compile 'com.github.liuguangqiang.SuperRecyclerView:super-recyclerview:0.1.2' compile files('libs/gson-2.3.1.jar')
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.myapplication"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:background="#fff" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/qx_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="15dp" android:drawableLeft="@drawable/shopcart_unselected" android:text="全选" android:textSize="22dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_gravity="center_vertical" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/zongjia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总价:0.0" /> <TextView android:id="@+id/geshu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="共0件商品" /> </LinearLayout> <TextView android:id="@+id/js_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="13dp" android:background="@drawable/jiesuan_btn" android:paddingBottom="10dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="10dp" android:text="去结算" android:textSize="24dp" /> </LinearLayout> </LinearLayout>
item_rv.xml
<?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/ll" android:layout_width="match_parent" android:layout_height="50dp"> <!--商铺选中的√--> <Button android:id="@+id/shop_cb" android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="center" android:layout_marginLeft="15dp" android:layout_marginRight="30dp" android:background="@drawable/shopcart_unselected" /> <!--商铺的图片--> <TextView android:id="@+id/shop_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:drawableLeft="@drawable/shopcart_shop" android:text="商铺" android:textSize="22dp" /> </LinearLayout> <LinearLayout android:layout_marginLeft="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/sp_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="78dp" android:layout_marginTop="10dp" android:text="商品" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!--商品选中的√--> <Button android:id="@+id/sp_cb" android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="center" android:layout_marginLeft="20dp" android:layout_marginRight="30dp" android:background="@drawable/shopcart_unselected" /> <ImageView android:id="@+id/sp_ima" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_vertical" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/sp_xj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="8dp" android:text="现价" android:textSize="18dp" /> <TextView android:id="@+id/sp_yj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="原价" android:textSize="18dp" /> <!--加减号 组合控件--> <app.com.myapplication.CountView<!--换成自己的CountView-->
android:id="@+id/cv"android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="5dp" /> </LinearLayout> <!--删除--> <ImageView android:id="@+id/shanchu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="30dp" android:src="@drawable/shopcart_delete" /> </LinearLayout> </LinearLayout></LinearLayout>
countview.xml
<?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"> <!--减号--> <Button android:id="@+id/gs_jian" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/shopcart_minus_grey" /> <!--中间显示的个数--> <TextView android:id="@+id/count" android:layout_width="50dp" android:layout_height="30dp" android:text="1" android:layout_marginLeft="20dp" android:textAlignment="center" android:textSize="20dp" /> <!--加号--> <Button android:id="@+id/gs_jia" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/shopcart_add_red" /> </LinearLayout>
bean包 ShopBean.java
package app.com.myapplication.bean; import java.util.List; /** * @author ddy */ public class ShopBean { /** * msg : 请求成功 * 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":85,"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"}] */ 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":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 * 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; //第一个 int isshop; //商铺 boolean shopsele; public boolean getShopsele() { return shopsele; } public void setShopsele(boolean shopsele) { this.shopsele = shopsele; } public int getisshop() { return isshop; } public void setisshop(int isshop) { this.isshop = isshop; } 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; } } } }
CountView.java
package app.com.myapplication; import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; /** * @author ddy */ public class CountView extends LinearLayout { int mcount = 1;//实际初始个数 private TextView count;//中间显示的个数 JJLicense jjLicense;//接口 public int getCount() { return mcount; } public void setCount(int mcount, JJLicense jjLicense) { this.mcount = mcount; this.jjLicense = jjLicense; count.setText("" + mcount); } public CountView(Context context) { super(context); } public CountView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //获得子布局 View view = LayoutInflater.from(context).inflate(R.layout.countview, null); Button gsJian = view.findViewById(R.id.gs_jian);//减 count = view.findViewById(R.id.count);//中间显示的个数 Button gsJia = view.findViewById(R.id.gs_jia);//加 gsJian.setOnClickListener(new OnClickListener() {//减号的点击事件 @Override public void onClick(View view) { mcount--;//实际个数减 1 if (mcount > 0) {//判断 个数>0 的时候 count.setText("" + mcount);//把实际个数 赋给 显示的个数 jjLicense.jjclick(mcount);//改变中间显示的个数 } else { //个数不大于0 mcount = 1; //实际个数就是 1 jjLicense.jjclick(mcount);//改变中间显示的个数 } } }); gsJia.setOnClickListener(new OnClickListener() {//加号的点击事件 @Override public void onClick(View view) { mcount++;//实际个数加 1 count.setText("" + mcount);//把实际个数 赋给 显示的个数 jjLicense.jjclick(mcount);//改变中间显示的个数 } }); addView(view); } public CountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } interface JJLicense {//修改中间显示的个数 //mcount中间显示的个数 void jjclick(int mcount); } }
MainActivity.java
package app.com.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import app.com.myapplication.bean.ShopBean; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { //RecyclerView @BindView(R.id.rv) RecyclerView rv; @BindView(R.id.qx_btn)//全选按钮 TextView qxBtn; @BindView(R.id.zongjia)//总价 TextView zongjia; @BindView(R.id.geshu)//个数 TextView geshu; @BindView(R.id.js_btn)//去结算 TextView jsBtn; private List<ShopBean.DataBean.ListBean> data = new ArrayList<>();//商品的集合 private List<ShopBean.DataBean> shop = new ArrayList<>();//商铺的集合 boolean allck = false; private MainActivity.rvAdapter rvAdapter;//适配器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); gethttp(); } private void gethttp() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://120.27.23.105/product/getCarts?uid=100").build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { //失败 } @Override public void onResponse(Call call, Response response) throws IOException { //成功 Gson gson = new Gson(); //把获得的值,赋给商铺 shop = gson.fromJson(response.body().string(), ShopBean.class).getData(); for (int i = 0; i < shop.size(); i++) { for (int j = 0; j < shop.get(i).getList().size(); j++) { //把商铺里的商品 添加到商品的集合里 data.add(shop.get(i).getList().get(j)); } } //排列 商品与商铺的显示位置 getShou(); runOnUiThread(new Runnable() { @Override public void run() { getzongshuju(); //计算商品个数和总价 //显示方式 VERTICAL false倒序 rv.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false)); rvAdapter = new rvAdapter(); rv.setAdapter(rvAdapter);//给RecyclerView赋值 } }); } }); } //计算商品个数和总价 private void getzongshuju() { double sum = 0;//初始价格总和 int zongshu = 0;//初始商品总数 for (int i = 0; i < data.size(); i++) { if (data.get(i).getSelected() == 1) {//判断 商品是否是选中状态 //获得选中商品数量 int i1 = data.get(i).getNum(); //价格总和 = 价格总和 + 廉价后的价格* 选中商品数量 sum = sum + data.get(i).getBargainPrice() * i1; //总个数=总个数+获得选中商品的数量 zongshu = zongshu + i1; } } //将得到的总价 转换成浮点型 赋给 zongjia(总价) zongjia.setText("总价:" + new DecimalFormat("0.00").format(sum)); geshu.setText("共" + zongshu + "件商品");//将得到的总个数赋给 geshu(个数) } //商品与商铺的排列 private void getShou() { data.get(0).setisshop(0);//获得第一个商品的第一个商铺 for (int i = 1; i < data.size(); i++) { //判断 商品的商铺id是否一样 if (data.get(i).getSellerid() == data.get(i - 1).getSellerid()) { //如果是一样,直接添加到前一个商品后面 data.get(i).setisshop(1); } else { //如果不是一样 成为新的商铺的第一个商品 data.get(i).setisshop(0); } } } @OnClick(R.id.qx_btn) public void onViewClicked() { allck = !allck;//全部选中,或者全部不选中 if (allck) { //判断 是否是全选中状态 //是全选中状态 赋给全选按钮的背景资源是 shopcart_selected 这个图片 qxBtn.setBackgroundResource(R.drawable.shopcart_selected); for (int i = 0; i < data.size(); i++) { //给商品的选中状态都变成 1 (就是选中状态) data.get(i).setSelected(1); data.get(i).setShopsele(true);//给商铺的选中状态都变成 true (就是选中状态) } } else { //不是全选中状态 赋给全选按钮的背景资源是 shopcart_unselected 这个图片 qxBtn.setBackgroundResource(R.drawable.shopcart_unselected); for (int i = 0; i < data.size(); i++) { data.get(i).setSelected(0);//给商品的选中状态都变成 0 (就是不选中状态) data.get(i).setShopsele(false);//给商铺的选中状态都变成 false (就是不选中状态) } } getzongshuju();//计算商品个数和总价 rvAdapter.notifyDataSetChanged();//刷新适配器 } class rvAdapter extends RecyclerView.Adapter<rvAdapter.ViewHolder> { @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //获得子布局 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, null); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { //判断 商品位置是否是商铺第一个 if (data.get(position).getisshop() == 0) { //是第一个的时候 在商品上面 显示出商铺 holder.linearLayout.setVisibility(View.VISIBLE); } else { //在商品上面 不显示出商铺 holder.linearLayout.setVisibility(View.GONE); } for (int i = 0; i < shop.size(); i++) { // 判断 商铺中的商铺id和 商品中的商铺id if (shop.get(i).getSellerid().equals("" + data.get(position).getSellerid())) { //把获得的商铺的名字 赋给 商铺 (获得商铺的名字) holder.shopName.setText(shop.get(i).getSellerName()); break; } } //判断商品中 商铺的选中状态 if (data.get(position).getShopsele()) {//商铺的选中状态是 true //商铺前面的选中框 赋给选中框的背景资源是 shopcart_selected 这个图片 holder.shopCb.setBackgroundResource(R.drawable.shopcart_selected); } else {//商铺的选中状态是 false //商铺前面的选中框 赋给选中框的背景资源是 shopcart_unselected 这个图片 holder.shopCb.setBackgroundResource(R.drawable.shopcart_unselected); } //判断 商品的选中状态是否是 1 if (data.get(position).getSelected() == 1) { //商品前面的选中框 赋给选中框的背景资源是 shopcart_selected 这个图片 holder.spCb.setBackgroundResource(R.drawable.shopcart_selected); } else if (data.get(position).getSelected() == 0) {//判断 商品的选中状态是否是 0 //商品前面的选中框 赋给选中框的背景资源是 shopcart_selected 这个图片 holder.spCb.setBackgroundResource(R.drawable.shopcart_unselected); } //图片的字符串截取 [0]指的是取截取的第一段字符串 String s = data.get(position).getImages().split("\\|")[0]; ImageLoader.getInstance().displayImage(s, holder.spIma); holder.spName.setText(data.get(position).getTitle()); holder.spXj.setText("现价:" + data.get(position).getBargainPrice()); holder.spYj.setText("原价:" + data.get(position).getPrice()); //加减按钮 holder.cv.setCount(data.get(position).getNum(), new CountView.JJLicense() { @Override public void jjclick(int mcount) { data.get(position).setNum(mcount);//把增加或者减小了的值赋给 对应的商品 getzongshuju();//计算商品个数和总价 } }); //商品的点击事件 holder.shopCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //商品的商铺状态 true变false false变true 反选 data.get(position).setShopsele(!data.get(position).getShopsele()); //子单选 for (int i = 0; i < data.size(); i++) { //判断 position下标的商品的商铺id == i 下标的商品的商铺id if (data.get(position).getSellerid() == data.get(i).getSellerid()) { if (data.get(position).getShopsele()) {//商品的商铺状态为 true data.get(i).setSelected(1);//商铺下 商品的选中状态是 1 } else { data.get(i).setSelected(0);//商铺下 商品的选中状态是 0 } } } getall();//检查一下是否全选 getzongshuju();//计算商品个数和总价 notifyDataSetChanged();//刷新适配器 } }); //商品的点击事件 holder.spCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (data.get(position).getSelected() == 0) {//判断商品的状态是否是 0 data.get(position).setSelected(1);//将商品的状态变为 1 data.get(position).setShopsele(true);//商铺的状态变为 true } else { data.get(position).setSelected(0);//将商品的状态变为 0 data.get(position).setShopsele(false);//商铺的状态变为 false } //子全选 for (int j = 0; j < data.size(); j++) { if (data.get(j).getisshop() == 0) { for (int i = 0; i < data.size(); i++) { if (data.get(j).getSellerid() == data.get(i).getSellerid() && data.get(i).getSelected() == 0) { data.get(j).setShopsele(false);//商铺的状态是 false break; } else { data.get(j).setShopsele(true);//商铺的状态是 true } } } } getall();//检查一下是否全选 getzongshuju();//计算商品个数和总价 notifyDataSetChanged();//刷新适配器 } }); //删除的监听事件 holder.shanchu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //移除下标是position的数据 data.remove(position); //重新展示商铺和商品 getShou(); //重新计算数量和价格 getzongshuju(); //刷新适配器 notifyDataSetChanged(); } }); } //全选 private void getall() { boolean all = true; for (int j = 0; j < data.size(); j++) { //循环获得数组的选中状态 判断是否存在等于0的(未选中) if (data.get(j).getSelected() == 0) { //如果有 则全选状态是未选中 all = false; } } //全选状态是true if (all) { qxBtn.setBackgroundResource(R.drawable.shopcart_selected); } else { //全选状态是false qxBtn.setBackgroundResource(R.drawable.shopcart_unselected); } } @Override public int getItemCount() { //数组的大小长度 return data.size(); } class ViewHolder extends RecyclerView.ViewHolder { //商铺前面的选中框 @BindView(R.id.shop_cb) Button shopCb; //商铺的名字 @BindView(R.id.shop_name) TextView shopName; //商品的名字 @BindView(R.id.sp_name) TextView spName; //商品前面的选中框 @BindView(R.id.sp_cb) Button spCb; //商品的图片 @BindView(R.id.sp_ima) ImageView spIma; //现价 @BindView(R.id.sp_xj) TextView spXj; //原价 @BindView(R.id.sp_yj) TextView spYj; //加减号 组合控件 @BindView(R.id.cv) CountView cv; private final LinearLayout linearLayout; //删除图片 private final ImageView shanchu; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); //设置商家的显示或者隐藏 (同一个商家只显示一次,不多次显示) linearLayout = view.findViewById(R.id.ll); //删除图片 shanchu = view.findViewById(R.id.shanchu); } } } }
MyApp.java
package app.com.myapplication; import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * @author ddy */ public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration imageLoaderConfiguration = ImageLoaderConfiguration.createDefault(getApplicationContext()); ImageLoader.getInstance().init(imageLoaderConfiguration); } }
相关文章推荐
- 二级列表+购物车布局
- ExpandableListView二级列表购物车,MVP获取数据
- 二级列表购物车
- 二级列表RecyclerView购物车
- 高仿淘宝 二级列表固定数据 购物车
- Andriod购物车实现二级列表(MVP)
- 购物车的二级列表全选单选
- 购物车二级列表
- 简单实现 二级列表购物车(固定数据)
- 二级列表展示购物车MVP
- Android 二级列表仿京东购物车 《H》
- RecyclerView 二级列表 购物车
- Android----仿京东购物车(二级列表,mvp请求数据)
- Andriodjie——二级列表实现购物车
- 二级列表购物车okhttp(完整版)
- 二级列表购物车
- 二级列表完美实现购物车
- android二级列表显示购物车
- 安卓二级列表购物车 略屌略屌 一个类足矣
- 二级列表RecyclerView购物车