public class MyAdapter extends BaseExpandableListAdapter{
private RelativeLayout relative_progress;
private CartPresenter cartPresenter;
private Handler handler;
private CartBean cartBean;
private Context context;
private int size;
private int childI;
private int allSize;
private int index;
public MyAdapter(Context context, CartBean cartBean, Handler handler, CartPresenter cartPresenter, RelativeLayout relative_progress) {
this.context = context;
this.cartBean = cartBean;
this.handler = handler;
this.cartPresenter = cartPresenter;
this.relative_progress = relative_progress;
}
@Override
public int getGroupCount() {
return cartBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
return cartBean.getData().get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return cartBean.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return cartBean.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 View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
final GroupHolder holder;
if (view == null){
view = View.inflate(context, R.layout.group_item_layout,null);
holder = new GroupHolder();
holder.check_group = view.findViewById(R.id.check_group);
holder.text_group = view.findViewById(R.id.text_group);
view.setTag(holder);
}else {
holder = (GroupHolder) view.getTag();
}
final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);
//赋值
holder.check_group.setChecked(dataBean.isGroupChecked());
holder.text_group.setText(dataBean.getSellerName());
//组的点击事件...也要去请求更新的接口
holder.check_group.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
relative_progress.setVisibility(View.VISIBLE);//显示
size = dataBean.getList().size();
childI = 0;
updateAllInGroup(holder.check_group.isChecked(),dataBean);
}
});
return view;
}
/**
* 更新一组的状态
* @param checked
* @param dataBean
*/
private void updateAllInGroup(final boolean checked, final CartBean.DataBean dataBean) {
CartBean.DataBean.ListBean listBean = dataBean.getList().get(childI);//0
//?uid=71&sellerid=1&pid=1&selected=0&num=10
Map<String, String> params = new HashMap<>();
params.put("uid","4240");
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(APIUtil.updateCartUrl, 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;//0,1,2...3
if (childI <size){
updateAllInGroup(checked,dataBean);
}else {
//所有的条目已经更新完成....再次请求查询购物车的数据
cartPresenter.getCartData(APIUtil.cartUrl);
}
}
}
});
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ChildHolder holder;
if (view == null){
view = View.inflate(context, R.layout.child_item_layout,null);
holder = new ChildHolder();
holder.text_add = view.findViewById(R.id.text_add);
holder.text_num = view.findViewById(R.id.text_num);
holder.text_jian = view.findViewById(R.id.text_jian);
holder.text_title = view.findViewById(R.id.text_title);
holder.text_price = view.findViewById(R.id.text_price);
holder.image_good = view.findViewById(R.id.image_good);
holder.check_child = view.findViewById(R.id.check_child);
holder.text_delete = view.findViewById(R.id.text_delete);
view.setTag(holder);
}else {
holder = (ChildHolder) view.getTag();
}
//赋值
final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);
holder.text_num.setText(listBean.getNum()+"");//......注意
holder.text_price.setText("¥"+listBean.getBargainPrice());
holder.text_title.setText(listBean.getTitle());
//listBean.getSelected().....0false,,,1true
//设置checkBox选中状态
holder.check_child.setChecked(listBean.getSelected()==0? false:true);
/*implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'*/
Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.image_good);
//点击事件
holder.check_child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击的时候 更新当前条目选中的状态,,,更新完之后,请求查询购物车,重新展示数据
relative_progress.setVisibility(View.VISIBLE);
updateChildChecked(listBean);
}
});
//加号
holder.text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//请求更新的接口
updateChildNum(listBean,true);
}
});
//减号
holder.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listBean.getNum() == 1){
return;
}
//更新数量,,,减
updateChildNum(listBean,false);
}
});
return view;
}
/**
* 更新数量
* @param listBean
* @param
*/
private void updateChildNum(CartBean.DataBean.ListBean listBean, boolean isAdded) {
//一旦执行更新的操作,,,progressBar显示
relative_progress.setVisibility(View.VISIBLE);
//?uid=71&sellerid=1&pid=1&selected=0&num=10
Map<String, String> params = new HashMap<>();
params.put("uid","4240");
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(listBean.getSelected()));
if (isAdded){
params.put("num", String.valueOf(listBean.getNum() + 1));
}else {
params.put("num", String.valueOf(listBean.getNum() - 1));
}
OkHttp3Util.doPost(APIUtil.updateCartUrl, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示
if (response.isSuccessful()){
cartPresenter.getCartData(APIUtil.cartUrl);
}
}
});
}
/**
* 更新子条目 网络上的状态
* @param listBean
*/
private void updateChildChecked(CartBean.DataBean.ListBean listBean) {
//一旦执行更新的操作,,,progressBar显示
relative_progress.setVisibility(View.VISIBLE);
//?uid=71&sellerid=1&pid=1&selected=0&num=10
Map<String, String> params = new HashMap<>();
params.put("uid","4240");
params.put("sellerid", String.valueOf(listBean.getSellerid()));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0));
params.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util.doPost(APIUtil.updateCartUrl, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示
if (response.isSuccessful()){
cartPresenter.getCartData(APIUtil.cartUrl);
}
}
});
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
/**
* 计算价格和数量 并发送显示
*/
public void sendPriceAndCount() {
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<cartBean.getData().size();i++){
for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){
if (cartBean.getData().get(i).getList().get(j).getSelected() == 1){
//价格是打折的价格...........
price += cartBean.getData().get(i).getList().get(j).getNum() * cartBean.getData().get(i).getList().get(j).getBargainPrice();
count += cartBean.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<CartBean.DataBean.ListBean> allList = new ArrayList<>();
for (int i=0;i<cartBean.getData().size();i++){
for (int j=0;j<cartBean.getData().get(i).getList().size();j++){
allList.add(cartBean.getData().get(i).getList().get(j));
}
}
relative_progress.setVisibility(View.VISIBLE);
allSize = allList.size();
index = 0;
//通过 递归 更新所有子条目的选中
updateAllChild(allList,checked);
}
/**
* 根据全选 跟新所有的子条目
* @param allList
* @param checked
*/
private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {
CartBean.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("uid","4240");
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(APIUtil.updateCartUrl, 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 < allSize){
updateAllChild(allList,checked);
}else {
//查询购物车
cartPresenter.getCartData(APIUtil.cartUrl);
}
}
}
});
}
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;
}
}
购物车适配器
最新推荐文章于 2019-04-06 20:26:48 发布