C层
public interface ShopContract { public interface ShopView{ public void showShopData(String message); } public interface ShopPresenter<ShopView>{ public void attachView(ShopView shopView); public void detachView(ShopView shopView); public void requsetShopData(); } public interface ShopModel{ public void containShopData(CallBack callBack); public interface CallBack{ public void reponseShopData(String message); } } }
P层
public class ShopModelImpl implements ShopContract.ShopModel { @Override public void containShopData(final CallBack callBack) { OkGo.<String>get(Constant.Shop).execute(new StringCallback() { @Override public void onSuccess(Response<String> response) { String reponseData = response.body().toString(); callBack.reponseShopData(reponseData); } }); } }
M层
public class ShopPresenterImpl implements ShopContract.ShopPresenter<ShopContract.ShopView> { ShopContract.ShopModel shopModel; ShopContract.ShopView shopView; private SoftReference<ShopContract.ShopView> softReference; @Override public void attachView(ShopContract.ShopView shopView) { this.shopView = shopView; softReference = new SoftReference<>(shopView); shopModel = new ShopModelImpl(); } @Override public void detachView(ShopContract.ShopView shopView) { softReference.clear(); } @Override public void requsetShopData() { shopModel.containShopData(new ShopContract.ShopModel.CallBack() { @Override public void reponseShopData(String message) { shopView.showShopData(message); } }); } }
创建Bean类
private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * list : [{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":72,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"},{"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":25,"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"},{"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":3,"pid":80,"price":777,"pscid":40,"selected":0,"sellerid":1,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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":29,"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; private boolean businessChecked; public boolean getBusinessChecked() { return businessChecked; } public void setBusinessChecked(boolean businessChecked) { this.businessChecked = businessChecked; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { /** * bargainPrice : 99.0 * createtime : 2017-10-14T21:38:26 * detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg * num : 72 * pid : 45 * price : 2999.0 * pscid : 39 * selected : 0 * sellerid : 1 * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽! * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机 */ 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; private boolean goodsChecked; private int defalutNumber = 1; public int getDefalutNumber() { return defalutNumber; } public void setDefalutNumber(int defalutNumber) { this.defalutNumber = defalutNumber; } public boolean getGoodsChecked() { return goodsChecked; } public void setGoodsChecked(boolean goodsChecked) { this.goodsChecked = goodsChecked; } 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; } } }
接口是这样的:
public static final String Shop = "https://www.zhaoapi.cn/product/getCarts?uid=71";
adapter适配器
public class BusinseeAdapter extends BaseQuickAdapter<Shop.DataBean,BaseViewHolder> { private RecyclerView recyclerView1; OnBussinessItemClickListener onBussinessItemClickListener; public void setOnBussinessItemClickListener(OnBussinessItemClickListener onBussinessItemClickListener){ this.onBussinessItemClickListener = onBussinessItemClickListener; } public interface OnBussinessItemClickListener{ public void onCallBack(); } public BusinseeAdapter(int layoutResId, @Nullable List<Shop.DataBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, final Shop.DataBean item) { //找到商家的控件 helper.setText(R.id.business_name,item.getSellerName()); //避免焦点抢占 final CheckBox checkBox = helper.getView(R.id.c_business); checkBox.setOnCheckedChangeListener(null); //条目是否选中状态 checkBox.setChecked(item.getBusinessChecked()); recyclerView1 = helper.getView(R.id.recyclerView1); //子商品条目数据 List<Shop.DataBean.ListBean> list = item.getList(); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false); final GoodsAdapter goodsAdapter = new GoodsAdapter(R.layout.frag_goods, list); recyclerView1.setLayoutManager(linearLayoutManager); recyclerView1.setAdapter(goodsAdapter); goodsAdapter.setOnGoodsItemClickListener(new GoodsAdapter.onGoodsItemClickListener() { @Override public void onCallBack() { boolean result = true; for (int i =0;i<item.getList().size();i++){ result = result&item.getList().get(i).getGoodsChecked(); } checkBox.setChecked(result); goodsAdapter.notifyDataSetChanged(); onBussinessItemClickListener.onCallBack(); } }); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i=0;i<item.getList().size();i++){ item.getList().get(i).setGoodsChecked(checkBox.isChecked()); } item.setBusinessChecked(checkBox.isChecked()); notifyDataSetChanged(); onBussinessItemClickListener.onCallBack(); } }); } }
public class GoodsAdapter extends BaseQuickAdapter<Shop.DataBean.ListBean,BaseViewHolder> { //设置接口回调 点击事件 onGoodsItemClickListener onGoodsItemClickListener; public void setOnGoodsItemClickListener(onGoodsItemClickListener onGoodsItemClickListener){ this.onGoodsItemClickListener = onGoodsItemClickListener; } public interface onGoodsItemClickListener{ public void onCallBack(); } public GoodsAdapter(int layoutResId, @Nullable List<Shop.DataBean.ListBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, final Shop.DataBean.ListBean item) { //找到控件 helper.setText(R.id.goodsTitle,item.getTitle()); helper.setText(R.id.goodsPrice,item.getPrice()+""); ImageView imageView = helper.getView(R.id.image); //图片切割 String imagesString = item.getImages(); String[] imageStr = imagesString.split("\\|"); Glide.with(mContext).load(imageStr[0]).into(imageView); //找到商品的输入框控件 CheckBox goods = helper.getView(R.id.goods); goods.setOnCheckedChangeListener(null); goods.setChecked(item.getGoodsChecked()); goods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { item.setGoodsChecked(isChecked); onGoodsItemClickListener.onCallBack(); } }); CaculatorView caculatorView = helper.getView(R.id.cv_caculatorView); caculatorView.setOnCaculatorListener(new CaculatorView.onCaculatorListener() { @Override public void dis(int number) { item.setDefalutNumber(number); onGoodsItemClickListener.onCallBack(); } @Override public void adds(int number) { item.setDefalutNumber(number); onGoodsItemClickListener.onCallBack(); } }); } }
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:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView_Zong" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总价是:" android:layout_marginLeft="20dp" /> <Button android:id="@+id/jie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结算" android:background="#f0f" android:layout_marginLeft="160dp" /> </LinearLayout> </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="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/business_all" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/business_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="店家" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView_dian" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout>
商品
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="10dp" > <CheckBox android:id="@+id/goods" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" /> <ImageView android:id="@+id/image" android:layout_width="80dp" android:layout_height="80dp" android:layout_toRightOf="@+id/goods" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_toRightOf="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:orientation="vertical" > <TextView android:id="@+id/goodsTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:hint="商品名称" android:maxEms="20" android:paddingLeft="5dp" android:textStyle="bold" /> <TextView android:gravity="center" android:id="@+id/goodsPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/goodsTitle" android:layout_marginTop="@dimen/dp_10" android:hint="商品价格" /> <com.example.zb.ui.wight.Caculator android:id="@+id/cv_caculatorView" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" > </com.example.zb.ui.wight.Caculator> </LinearLayout> </RelativeLayout> </LinearLayout>
加减器
public class Caculator extends LinearLayout implements View.OnClickListener { private Button jia; private TextView number1; private Button jian; public Caculator(Context context) { super(context); } public Caculator(Context context, AttributeSet attrs) { super(context, attrs); } public Caculator(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = LayoutInflater.from(context).inflate(R.layout.caculator,this); jia = view.findViewById(R.id.jia); number1 =view.findViewById(R.id.number); jian = view.findViewById(R.id.jian); jia.setOnClickListener(this); jian.setOnClickListener(this); } @Override public void onClick(View v) { String numberString = number1.getText().toString(); int number = Integer.parseInt(numberString); switch (v.getId()){ case R.id.jian: number = number-1; if (number<0){ number=0; number1.setText(String.valueOf(number)); } number1.setText(String.valueOf(number)); onCaculatorListener.dis(number); break; case R.id.jia: number = number+1; number1.setText(String.valueOf(number)); onCaculatorListener.adds(number); break; } } onCaculatorListener onCaculatorListener; public interface onCaculatorListener{ public void dis(int number); public void adds(int number); } public void setOnCaculatorListener(Caculator.onCaculatorListener onCaculatorListener) { this.onCaculatorListener = onCaculatorListener; } }
布局
<?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" > <Button android:id="@+id/jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:background="#fff" android:textSize="30sp" /> <TextView android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:text="1" /> <Button android:id="@+id/jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:background="#fff" android:textSize="30sp" /> </LinearLayout>