安卓开发|仿京东购物车

简单的说下思路1,适配器器中的getGroupView的方法中,老样子查找控件复用就不说了,接下来是对控件赋值,这一组的checkbox的点击事件,点击后获得一下子条目集合的长度,初始化一个int变量,用来累计更新的子条目的个数,根据组的状态更新组里边的状态----方法中传数据的集合和当前checkbox的状态例如------ updateChildInGroup(dataBean,holder.check_group.isChecked()); 2.在getChildview中也是一样赋值完成后,添加子条目的点击事件,调用更新购物车的方法 1方法中调用更新购物车的接口,----通过post方式请求,加上拼接的参数,除了字段中的状态是-------拿组的状态给他设置1或0,1为选中,0为未选 解释一下:因为如果一个组选中了,那么它里边的所有子条目都要选中所以给他设置3.--如果更新成功,也就是访问网络成功,那么代表着更新完一条,让记录状态的值加一,判断如果小于集合长度,自己调用自己,继续更新,直到else把所有子条目更新完,else就是全部更新完成,重新访问查询购物车的方法//注意,,数量要转换字符串价格拿getBargainPrice()设置子条目的状态,要根据里边的0或1来设置true 和false注意:---4.//点击的时候调用更新网络购物车的接口,调用更新的方法,因为多个方法调用,所以传了连个参数来区分执行什么方法,(数据,true为更新购物叉车,false),false,true为数量增加,false 为数量减少,更新的的访问网络请求执行完判断成功后重新查询购物车,//5.点击全选的时候,调用方法,传一个当前的checkbox的状态布尔值到方法中,方法中将通过遍历所有子条目放到一个集合,初始化一个int变量记录更新了几条,调用更新所用子条目的方法,将这个集合和全选checkbox的状态发过去,接下来是访问更新购物车的接口,注意selected参数根据状态要设置1或0去更新购物车,判断访问执行成功,也就是访问网络成功,那么代表着更新完一条,让记录状态的值加一,判断如果小于集合长度,自己调用自己,继续更新,直到else把所有子条目更新完,else就是全部更新完成,重新访问查询购物车的方法计算商品总价格的方法 初始化变量,double价格int 数量通过循环判断二级列表是否勾选判断是否计算价格,如果子条目是选中,计算总价和数量,通过循环累加,最后通过handler发送到activity显示Activity中的请求网络之前显示加载,然后请求网络,完成时,通过循环判断组中的所有子条目的状态来设置时候选中状态,让偶判断所有组是否选中,来决定全选是否选中,设置适配器,展开所有组,然后初始化价格,调用适配器中计算价格的方法设置点击删除的,获得当前子条目集合,请求删除的接口,必填参数获得当前集合的pid也就是根据商品的id删除的,请求成功后,重新调用查询的activity中查询的方法接下来看代码public class MyExpandableAdapter extends BaseExpandableListAdapter {Context context; QueryBean queryBean; RelativeLayout relativeLayout; private int childsize; private int childI; Handler handler; private int index; private int allchildsize; public MyExpandableAdapter(Context context, QueryBean queryBean, RelativeLayout relativeLayout, Handler handler) { this.context=context; this.queryBean=queryBean; this.relativeLayout = relativeLayout; this.handler=handler; }@Override public int getGroupCount() { return queryBean.getData().size(); }@Override public int getChildrenCount(int groupPosition) { return queryBean.getData().get(groupPosition).getList().size(); }@Override public Object getGroup(int groupPosition) { return queryBean.getData().get(groupPosition); }@Override public Object getChild(int groupPosition, int childPosition) { return queryBean.getData().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 boolean isChildSelectable(int groupPosition, int childPosition) { return true; }//设置group的 @Override public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) { //获得集合 List groupList = queryBean.getData(); final GroupHolder groupHolder; if (view==null){ view = View.inflate(context, R.layout.group_item_layout,null); groupHolder = new GroupHolder(); groupHolder.check_group=view.findViewById(R.id.group_check); groupHolder.text_group=view.findViewById(R.id.grouptitle); view.setTag(groupHolder); }else{ groupHolder = (GroupHolder) view.getTag(); }//找到集合中的一个 final QueryBean.DataBean dataBean = groupList.get(groupPosition); //赋值 groupHolder.check_group.setChecked(dataBean.isGroupChecked()); groupHolder.text_group.setText(dataBean.getSellerName()); //设置一级的点击事件groupHolder.check_group.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//点击的时候让进度条显示 relativeLayout.setVisibility(View.VISIBLE); //显示//得到子条目集合的长度 childsize = dataBean.getList().size(); //用来存放子条目更新的个数 childI = 0; //更新购物车,访问购物车的接口 updateAllInGroup(groupHolder.check_group.isChecked(),dataBean); //Toast.makeText(context,"当前状态"+groupHolder.check_group.isChecked(),Toast.LENGTH_SHORT).show(); } }); return view; } private void updateAllInGroup(final boolean checked, final QueryBean.DataBean dataBean) { //拿到子条目集合 QueryBean.DataBean.ListBean listBean = dataBean.getList().get(childI); Map params = new HashMap<>(); params.put("uid","4575"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(checked? 1:0)); Okhttp3Util.doPost(API.updatacart, params, new Callback() { @Override public void onFailure(Call call, IOException e) {}@Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ childI=childI+1; //如果小于,则递归 if (childI(); params.put("uid","4575"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(listBean.getSelected())); Toast.makeText(context,"当前状态"+upstate,Toast.LENGTH_SHORT).show(); params.put("num",listBean.getNum()+""); if (upstate){ params.put("selected", String.valueOf(listBean.getSelected()==0? 1:0)); }else{if (addorjian){ params.put("num",listBean.getNum()+1+""); }else{ params.put("num",listBean.getNum()-1+""); }} Okhttp3Util.doPost(API.updatacart, params, new Callback() { @Override public void onFailure(Call call, IOException e) {}@Override public void onResponse(Call call, Response response) throws IOException { //否则查询购物车 ShopPingCartActivity shopPingCartActivity = (ShopPingCartActivity) context; shopPingCartActivity.getNetData(API.queryCartApi); } }); }/** * 计算价格和数量 并发送显示 */ public void sendPriceAndCount() {double price = 0; int count = 0; //通过判断二级列表是否勾选,,,,计算价格数量 for (int i=0; i allList = new ArrayList<>(); for (int i=0; i allList, final boolean checked) {QueryBean.DataBean.ListBean listBean = allList.get(index); //0//跟新的操作 //?uid=71&sellerid=1&pid=1&selected=0&num=10 Map params = new HashMap<>(); params.put("num",listBean.getNum()+1+""); params.put("uid","4575"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("selected", String.valueOf(checked ? 1:0)); params.put("num", String.valueOf(listBean.getNum())); Okhttp3Util.doPost(API.updatacart, params, new Callback() { @Override public void onFailure(Call call, IOException e) {}@Override public void onResponse(Call call, Response response) throws IOException {if (response.isSuccessful()){ index = index +1; //0,1,2......3 if (index < allchildsize){updateAllChild(allList,checked); }else { //查询购物车 ShopPingCartActivity shopPingCartActivity = (ShopPingCartActivity) context; //更新完成后调用查询购物车的方法 shopPingCartActivity.getNetData(API.queryCartApi); } } } }); }//viewHolder private class GroupHolder{ CheckBox check_group; TextView text_group; }private class ChildHolder{ CheckBox check_child; ImageView image_good; TextView text_title; TextView text_price; TextView text_jian; TextView text_num; TextView text_add; TextView text_delete; }}



适配器



public class ShopPingCartActivity extends AppCompatActivity implements View.OnClickListener {private ExpandableListView expandablelistview; private CheckBox chackAll; private MyExpandableAdapter adapter; private RelativeLayout relativeLayout; private TextView text_heji; private Button text_jiesuan; private RelativeLayout relativelayout_cont; public QueryBean queryBean; //创建handler @SuppressLint("HandlerLeak") Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 0){ CountPriceBean countPriceBean = (CountPriceBean) msg.obj; text_heji.setText("合计:¥"+countPriceBean.getPriceString()); text_jiesuan.setText("去结算("+countPriceBean.getCount()+")"); }else if (msg.what==1){ queryBean = (QueryBean) msg.obj; relativeLayout.setVisibility(View.GONE); if(queryBean!=null) { relativelayout_cont.setVisibility(View.VISIBLE); //判断当前组中子条目是否选中,来显示组的显示状态 for (int i = 0; i < queryBean.getData().size(); i++) { if (isAllChildInGroupSelected(i)) { Log.d("tag","组里面所有子条目的状态"+isAllChildInGroupSelected(i)); queryBean.getData().get(i).setGroupChecked(true); } } //设置全选状态,去判断所有组是否选中 chackAll.setChecked(isAllGroupChecked()); //设置适配器 setAdapter(); //展开所有组 for (int i = 0; i < queryBean.getData().size(); i++) { expandablelistview.expandGroup(i); }//计算价格数量 adapter.sendPriceAndCount(); } }else{ //隐藏下面的全选.... 等 relativelayout_cont.setVisibility(View.GONE); //显示去逛逛,,,添加购物车 Toast.makeText(ShopPingCartActivity.this,"购物车为空,去逛逛",Toast.LENGTH_SHORT).show(); } } }; //设置适配器的方法 private void setAdapter() {adapter = new MyExpandableAdapter(ShopPingCartActivity.this, queryBean,relativeLayout,handler); expandablelistview.setAdapter(adapter); }@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop_ping_cart); //查找控件 expandablelistview = findViewById(R.id.expandablelistview); chackAll = findViewById(R.id.chackAll); relativeLayout = findViewById(R.id.relative_progress); text_heji = findViewById(R.id.text_heji); text_jiesuan = findViewById(R.id.text_jiesuan); relativelayout_cont = findViewById(R.id.relativelayout_cont); //去掉默认指示器 expandablelistview.setGroupIndicator(null); //全选反选的点击事件 chackAll.setOnClickListener(this); //点击结算的点击事件 text_jiesuan.setOnClickListener(this); }@Override protected void onResume() { super.onResume(); //显示进度 relativeLayout.setVisibility(View.VISIBLE); //获取网络 String url = API.queryCartApi; getNetData(url); }//请求网络 public void getNetData(String url) {Map parmes = new HashMap<>(); parmes.put("uid","4575"); Okhttp3Util.doPost(url,parmes,new Callback() { @Override public void onFailure(Call call, IOException e) {}@Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String string = response.body().string(); QueryBean queryBean = new Gson().fromJson(string, QueryBean.class); Message msg = Message.obtain(); msg.what=1; msg.obj=queryBean; handler.sendMessage(msg); } } }); }/** * 判断当前组里面所有的子条目是否选中 * @param groupPosition * @return */ private boolean isAllChildInGroupSelected(int groupPosition) { for (int i= 0; i

case R.id.text_jiesuan: //获得价格 final String priceString = countPriceBean.getPriceString(); // Map params = new HashMap<>(); params.put("uid","3690"); params.put("price", String.valueOf(priceString)); //请求 Okhttp3Util.doPost(API.bottomorderApi, params, new Callback() { @Override public void onFailure(Call call, IOException e) {}@Override public void onResponse(Call call, Response response) throws IOException { //判断 if (response.isSuccessful()){ String string = response.body().string(); //解析 CreateOrderBean createOrderBean = new Gson().fromJson(string, CreateOrderBean.class); Message msg = Message.obtain(); PriceCreataOrder priceCreataOrder = new PriceCreataOrder(createOrderBean,priceString); msg.what=2; msg.obj= priceCreataOrder; handler.sendMessage(msg); }} }); break;


} }}




【安卓开发|仿京东购物车】

    推荐阅读