一,创建新项目,导入依赖价包,
1网络权限
<uses-permission android:name="android.permission.INTERNET"/>
2,各种依赖
compile files('libs/gson-2.2.4.jar') compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.jakewharton:butterknife:7.0.1' compile files('libs/universal-image-loader-1.9.3-with-sources.jar')3.布局创建,Activity布局写入,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.shaping.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/third_recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/third_pay_linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/third_allselect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_10dp" android:drawableLeft="@drawable/shopcart_unselected" android:drawablePadding="@dimen/padding_5dp" android:text="全选" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/third_totalprice" android:layout_width="200dp" android:layout_height="wrap_content" android:paddingLeft="@dimen/padding_10dp" android:paddingTop="@dimen/padding_10dp" android:text="总价:" android:textColor="#000000" android:textSize="@dimen/common_font_size_16" /> <TextView android:id="@+id/third_totalnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="@dimen/padding_10dp" android:paddingLeft="@dimen/padding_10dp" android:text="共0件商品" android:textColor="#000000" android:textSize="@dimen/common_font_size_14" /> </LinearLayout> <TextView android:id="@+id/third_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="@dimen/margin_10dp" android:background="@drawable/login_btn" android:paddingBottom="@dimen/padding_10dp" android:paddingLeft="@dimen/margin_30dp" android:paddingRight="@dimen/margin_30dp" android:paddingTop="@dimen/padding_10dp" android:text="去结算" android:textColor="#000000" /> </LinearLayout> </LinearLayout>4.customview布局的设置写入。
<?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/revserse" android:layout_width="10dp" android:layout_height="wrap_content" android:background="#00FFFFFF" android:text="-" /> <EditText android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:id="@+id/add" android:layout_width="10dp" android:layout_height="wrap_content" android:background="#00FFFFFF" android:text="+" /> </LinearLayout>5, shop_adapter布局写入
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/shopface" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/danjia" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.example.shaping.CustomView android:id="@+id/customviewid" android:layout_width="100dp" android:layout_height="50dp" /> </LinearLayout> <Button android:id="@+id/shop_btn_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> </LinearLayout> </LinearLayout>6.接着就是代码的写入·MyActivity的构造。
import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import butterknife.Bind; 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 Activity { @Bind(R.id.third_recyclerview) RecyclerView thirdRecyclerview; @Bind(R.id.third_allselect) TextView thirdAllselect; @Bind(R.id.third_totalprice) TextView thirdTotalprice; @Bind(R.id.third_totalnum) TextView thirdTotalnum; @Bind(R.id.third_submit) TextView thirdSubmit; @Bind(R.id.third_pay_linear) LinearLayout thirdPayLinear; //模拟网络 //private List<ShopBean.OrderDataBean.CartlistBean> mAllOrderList = new ArrayList<>(); // private List<ShopDataBean.DataBean.ListBean> mAllOrderList = new ArrayList<>(); private ShopAdapter adapter; private LinearLayoutManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); getData(); // 1 为选中 2 选中 thirdAllselect.setTag(1); manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); adapter = new ShopAdapter(this); thirdRecyclerview.setLayoutManager(manager); thirdRecyclerview.setAdapter(adapter); //本地数据可直接添加数据 //网络请求使用(异步),直接添加数据为空,异步获取数据源后再添加数据 // adapter.add(mAllOrderList); adapter.setCheckBoxListener(new ShopAdapter.CheckBoxListener() { @Override public void check(int position, int count, boolean check, List<ShopDataBean.DataBean.ListBean> list) { sum(list); } }); adapter.setCustomViewListener(new ShopAdapter.CustomViewListener() { @Override public void click(int count, List<ShopDataBean.DataBean.ListBean> list) { sum(list); } }); adapter.setDelListener(new ShopAdapter.DelListener() { @Override public void del(int position, List<ShopDataBean.DataBean.ListBean> list) { sum(list); } }); } float price = 0; int count; /** * 计算总价 * * @param mAllOrderList */ private void sum(List<ShopDataBean.DataBean.ListBean> mAllOrderList) { price = 0; count = 0; boolean allCheck = true; for (ShopDataBean.DataBean.ListBean bean : mAllOrderList) { if (bean.isCheck()) { //得到总价 price += bean.getPrice() * bean.getNum(); //得到商品个数 count += bean.getNum(); } else { // 只要有一个商品未选中,全选按钮 应该设置成 为选中 allCheck = false; } } thirdTotalprice.setText("总价: " + price); thirdTotalnum.setText("共" + count + "件商品"); if (allCheck) { thirdAllselect.setTag(2); thirdAllselect.setBackgroundResource(R.drawable.shopcart_selected); } else { thirdAllselect.setTag(1); thirdAllselect.setBackgroundResource(R.drawable.shopcart_unselected); } } public void getData() { // //模拟网络请求 // InputStream inputStream = getAssets().open("shop.json"); // final String data = convertStreamToString(inputStream); // final Gson gson = new Gson(); // ShopBean shopBean = gson.fromJson(data, ShopBean.class); // for (int i = 0; i < shopBean.getOrderData().size(); i++) { // int length = shopBean.getOrderData().get(i).getCartlist().size(); // for (int j = 0; j < length; j++) { // mAllOrderList.add(shopBean.getOrderData().get(i).getCartlist().get(j)); // } // } //网络数据 OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://120.27.23.105/product/getCarts?uid=100") .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson1 = new Gson(); List<ShopDataBean.DataBean> data1 = gson1.fromJson(response.body().string(), ShopDataBean.class).getData(); for (int i = 0; i < data1.size(); i++) { for (int j = 0; j < data1.get(i).getList().size(); j++) { mAllOrderList.add(data1.get(i).getList().get(j)); } } //更新ui,适配器添加数据 runOnUiThread(new Runnable() { @Override public void run() { adapter.add(mAllOrderList); } }); } }); } public static String convertStreamToString(InputStream is) { /* * To convert the InputStream to String we use the BufferedReader.readLine() * method. We iterate until the BufferedReader return null which means * there's no more data to read. Each line will appended to a StringBuilder * and returned as String. */ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } boolean select = false; @OnClick(R.id.third_allselect) public void onClick() { //全选按钮 点击事件 int tag = (Integer) thirdAllselect.getTag(); if (tag == 1) { thirdAllselect.setTag(2); select = true; } else { thirdAllselect.setTag(1); select = false; } for (ShopDataBean.DataBean.ListBean bean : mAllOrderList) { bean.setCheck(select); } // for (ShopBean.OrderDataBean.CartlistBean bean : mAllOrderList) { // bean.setCheck(select); // } adapter.notifyDataSetChanged(); sum(adapter.getList()); } }7,MyApp类的实现。
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); } }
8.ShopDataBean数据类的实现 。/** * 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":83,"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.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; private boolean check; 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 boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } } } }9.ShopAdapter适配器类的实现。这样就能实现购物车效果了。import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; /** * Created by muhanxi on 17/11/15. */ public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.IViewHolder> { private Context context; //private List<ShopBean.OrderDataBean.CartlistBean> list ; private List<ShopDataBean.DataBean.ListBean> list; public ShopAdapter(Context context) { this.context = context; } /** * 更新数据 * * @param list */ public void add(List<ShopDataBean.DataBean.ListBean> list) { if (this.list == null) { this.list = new ArrayList<>(); } this.list.addAll(list); notifyDataSetChanged(); } // /** // * 更新数据 // * @param list // */ // public void add(List<ShopBean.OrderDataBean.CartlistBean> list){ // if(this.list == null){ // this.list = new ArrayList<>(); // } // this.list.addAll(list); // notifyDataSetChanged(); // } @Override public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.shop_adapter, null); return new IViewHolder(view); } @Override public void onBindViewHolder(final IViewHolder holder, final int position) { //防止checkbox 滑动 错乱 holder.checkbox.setChecked(list.get(position).isCheck()); holder.danjia.setText("" + list.get(position).getPrice()); //ImageLoader.getInstance().displayImage(list.get(position).getDefaultPic(), holder.shopface); String string = list.get(position).getImages().split("\\|")[0]; ImageLoader.getInstance().displayImage(string, holder.shopface); /** * checkbox 点击事件 */ holder.checkbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.get(position).setCheck(holder.checkbox.isChecked()); notifyDataSetChanged(); if (checkBoxListener != null) { checkBoxListener.check(position, holder.customviewid.getCurrentCount(), holder.checkbox.isChecked(), list); } } }); /** * 加减监听 */ holder.customviewid.setListener(new CustomView.ClickListener() { @Override public void click(int count) { //更新数据源 //list.get(position).getNum(count); list.get(position).setNum(count); notifyDataSetChanged(); if (listener != null) { listener.click(count, list); } } }); /** * 删除点击事件 */ holder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.remove(position); notifyDataSetChanged(); if (delListener != null) { delListener.del(position, list); } } }); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } static class IViewHolder extends RecyclerView.ViewHolder { @Bind(R.id.checkbox) CheckBox checkbox; @Bind(R.id.shopface) ImageView shopface; @Bind(R.id.danjia) TextView danjia; @Bind(R.id.customviewid) CustomView customviewid; @Bind(R.id.shop_btn_del) Button del; IViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } public List<ShopDataBean.DataBean.ListBean> getList() { return list; } CheckBoxListener checkBoxListener; /** * checkbox 点击事件 * * @param listener */ public void setCheckBoxListener(CheckBoxListener listener) { this.checkBoxListener = listener; } interface CheckBoxListener { public void check(int position, int count, boolean check, List<ShopDataBean.DataBean.ListBean> list); } CustomViewListener listener; /** * 加减号 点击事件 * * @param listener */ public void setCustomViewListener(CustomViewListener listener) { this.listener = listener; } interface CustomViewListener { public void click(int count, List<ShopDataBean.DataBean.ListBean> list); } DelListener delListener; /** * 加减号 删除按钮事件 * * @param listener */ public void setDelListener(DelListener listener) { this.delListener = listener; } interface DelListener { public void del(int position, List<ShopDataBean.DataBean.ListBean> list); } }10,CustomView 类的实现
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.EditText; import android.widget.LinearLayout; /** * Created by muhanxi on 17/11/15. */ public class CustomView extends LinearLayout { private EditText editText; private Button revserse; private Button add; private int mCount = 1 ; public CustomView(Context context) { super(context); } public CustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.customview,null,false); revserse = (Button) view.findViewById(R.id.revserse); add = (Button) view.findViewById(R.id.add); editText = (EditText) view.findViewById(R.id.content); revserse.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //减号 try { String content = editText.getText().toString().trim() ; int count = Integer.valueOf(content)-1; mCount = count; if(count > 1){ editText.setText(count+""); } if(listener != null){ listener.click(count); } } catch (NumberFormatException e) { e.printStackTrace(); } } }); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //加号 try { String content = editText.getText().toString().trim() ; int count = Integer.valueOf(content)+1; mCount = count; editText.setText(count+""); if(listener != null){ listener.click(count); } } catch (NumberFormatException e) { e.printStackTrace(); } } }); addView(view); } public int getCurrentCount(){ return mCount ; } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ClickListener listener; public void setListener(ClickListener listener){ this.listener = listener; } /** * 加减号 点击事件 */ public interface ClickListener { public void click(int count); } }11,注意Main文件夹必须加入本地数据assets,否则无法显示数据。