简单的说下思路
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<QueryBean.DataBean> 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<String,String> 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<childsize){
updateAllInGroup(checked,dataBean);
}else{
//否则查询购物车
ShopPingCartActivity shopPingCartActivity = (ShopPingCartActivity) context;
shopPingCartActivity.getNetData(API.queryCartApi);
}
}
}
});
}
//设置child
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
ChildHolder childHolder;
if (view == null) {
view = View.inflate(context, R.layout.child_item_layout, null);
childHolder = new ChildHolder();
childHolder.check_child = view.findViewById(R.id.child_check);
childHolder.image_good = view.findViewById(R.id.child_ima);
childHolder.text_title = view.findViewById(R.id.child_title);
childHolder.text_price = view.findViewById(R.id.child_price);
childHolder.text_jian = view.findViewById(R.id.jian);
childHolder.text_add = view.findViewById(R.id.add);
childHolder.text_num = view.findViewById(R.id.num);
childHolder.text_delete = view.findViewById(R.id.child_delete);
//复用
view.setTag(childHolder);
} else {
childHolder = (ChildHolder) view.getTag();
}
final QueryBean.DataBean.ListBean listBean = queryBean.getData().get(groupPosition).getList().get(childPosition);
//赋值
childHolder.text_title.setText(listBean.getTitle());
childHolder.text_price.setText(listBean.getBargainPrice()+"");
childHolder.text_num.setText(listBean.getNum()+"");
childHolder.check_child.setChecked(listBean.getSelected()==0? false:true);
Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childHolder.image_good);
//设置点击事件
childHolder.check_child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateCart(listBean,true,false);
}
});
//减号
childHolder.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateCart(listBean,false,false);
}
});
//加号
childHolder.text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listBean.getNum()==1){
return;
}else{
updateCart(listBean,false,false);
}
}
});
return view;
}
//更新购物车的
private void updateCart(QueryBean.DataBean.ListBean listBean, boolean upstate, boolean addorjian) {
Map<String, String> 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(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<queryBean.getData().size();i++){
for (int j = 0;j<queryBean.getData().get(i).getList().size();j++){
if (queryBean.getData().get(i).getList().get(j).getSelected() == 1){
//价格是打折的价格...........
price += queryBean.getData().get(i).getList().get(j).getNum() * queryBean.getData().get(i).getList().get(j).getBargainPrice();
count += queryBean.getData().get(i).getList().get(j).getNum();
}
}
}
//精准的保留double的两位小数
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String priceString = decimalFormat.format(price);
CountPriceBean countPriceBean = new CountPriceBean(priceString, count);
//发送...显示
Message msg = Message.obtain();
msg.what = 0;
msg.obj = countPriceBean;
handler.sendMessage(msg);
}
//
/**
* 根据全选的状态,,,,跟新每一个子条目的状态,,,全部更新完成后,查询购物车的数据进行展示
* @param checked
*/
public void setAllChildState(boolean checked) {
//创建一个集合 装所有的子条目
List<QueryBean.DataBean.ListBean> allList = new ArrayList<>();
for (int i=0;i<queryBean.getData().size();i++){
for (int j=0;j<queryBean.getData().get(i).getList().size();j++){
allList.add(queryBean.getData().get(i).getList().get(j));
}
}
relativeLayout.setVisibility(View.VISIBLE);
allchildsize = allList.size();
index = 0;
//通过 递归 更新所有子条目的选中
updateAllChild(allList,checked);
}
/**
* 根据全选 跟新所有的子条目
* @param allList
* @param checked
*/
private void updateAllChild(final List<QueryBean.DataBean.ListBean> allList, final boolean checked) {
QueryBean.DataBean.ListBean listBean = allList.get(index);//0
//跟新的操作
//?uid=71&sellerid=1&pid=1&selected=0&num=10
Map<String, String> 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<String,String> 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<queryBean.getData().get(groupPosition).getList().size();i++){ //只要有一个没选中就返回false if (queryBean.getData().get(groupPosition).getList().get(i).getSelected() ==0){ return false; } } return true; } /** * 所有的一级列表是否选中 */ private boolean isAllGroupChecked() { for (int i =0;i<queryBean.getData().size();i++){ if (queryBean.getData().get(i).isGroupChecked()==false){//代表一级列表有没选中的 Log.d("+++++","一级的状态"+queryBean.getData().get(i).isGroupChecked()); return false; } } return true; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.chackAll: adapter.setAllChildState(chackAll.isChecked()); Toast.makeText(ShopPingCartActivity.this,"全选",Toast.LENGTH_SHORT).show(); break;
case R.id.text_jiesuan: //获得价格 final String priceString = countPriceBean.getPriceString(); // Map<String, String> 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;
} }}