购物车

//适配器
public class ShopAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ShopBean arr;
private CheckBox mcCheckBox;
private TextView mTextView;

private float priceAll=0;

public void setMcCheckBox(CheckBox mcCheckBox) {
    this.mcCheckBox = mcCheckBox;
    mcCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkBox1= (CheckBox) v;
            sumprice(checkBox1.isChecked());
            SelectAll(checkBox1.isChecked());
        }
    });
}
private void SelectAll(boolean checked) {
    for (int i = 0; i < arr.getData().size(); i++) {
        ShopBean.DataBean dataBean = arr.getData().get(i);
        dataBean.setCheck_group(checked);
        for (int j = 0; j < dataBean.getList().size(); j++) {
            ShopBean.DataBean.ListBean listBean = dataBean.getList().get(j);
            listBean.setCheck_Child(checked);
        }
    }
    notifyDataSetChanged();
}
private void sumprice(boolean check) {
    priceAll=0;
    for (int i = 0; i < arr.getData().size(); i++) {
        for (int j = 0; j < arr.getData().get(i).getList().size(); j++) {
            double price = arr.getData().get(i).getList().get(j).getPrice();
            if (check){
                priceAll+=price;
            }else {
                priceAll=0;
            }
        }
    }
    mTextView.setText("总价:¥"+priceAll);
}
public void setmTextView(TextView mTextView) {
    this.mTextView = mTextView;
}

public ShopAdapter(Context mcContext) {
    this.mContext = mcContext;
}

public void setArr(ShopBean arr) {
    this.arr = arr;
    notifyDataSetChanged();
}

@Override
public int getGroupCount() {
    if(arr==null){
        return 0;
    }
    return arr.getData().size();
}

@Override
public int getChildrenCount(int groupPosition) {
    if(arr==null){
        return 0;
    }
    return arr.getData().get(groupPosition).getList().size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ViewHolder vh;
    if (convertView==null){
        vh=new ViewHolder();
        convertView=View.inflate(mContext, R.layout.group_shop_item,null);
        vh.checkBox=convertView.findViewById(R.id.checkbox_group);
        vh.name=convertView.findViewById(R.id.name_group);
        convertView.setTag(vh);
    }else {
        vh= (ViewHolder) convertView.getTag();
    }
    vh.checkBox.setChecked(arr.getData().get(groupPosition).isCheck_group());
    vh.name.setText(arr.getData().get(groupPosition).getSellerName());

    vh.checkBox.setTag(groupPosition);
    vh.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkBox= (CheckBox) v;
            boolean checked = checkBox.isChecked();
            int gpostion=Integer.parseInt(checkBox.getTag().toString());
            for (int i = 0; i < arr.getData().get(gpostion).getList().size(); i++) {
                double price = arr.getData().get(gpostion).getList().get(groupPosition).getPrice();
                if (checked){
                    priceAll+=price;
                }else {
                    priceAll+=price;
                }
            }
            mTextView.setText("总价:¥"+priceAll);
            arr.getData().get(gpostion).setCheck_group(checked);
            SelectGroup(gpostion,checked);
            mcCheckBox.setChecked(isSelectAllGroup());
            notifyDataSetChanged();
        }


    });
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ViewHolder2 vh;
    if (convertView==null){
        vh=new ViewHolder2();
        convertView=View.inflate(mContext, R.layout.child_shop_item,null);
        vh.checkBox=convertView.findViewById(R.id.checkbox_Child);
        vh.imageView=convertView.findViewById(R.id.img_Child);
        vh.name=convertView.findViewById(R.id.name_Child);
        vh.price=convertView.findViewById(R.id.price_Child);
        convertView.setTag(vh);
    }else {
        vh= (ViewHolder2) convertView.getTag();
    }
    vh.price.setText("¥:"+arr.getData().get(groupPosition).getList().get(childPosition).getPrice());
    vh.name.setText(arr.getData().get(groupPosition).getList().get(childPosition).getTitle());
    vh.checkBox.setChecked(arr.getData().get(groupPosition).getList().get(childPosition).isCheck_Child());
    Glide.with(mContext).load(arr.getData().get(groupPosition).getList().get(childPosition).getImages()).into(vh.imageView);

    vh.checkBox.setTag(groupPosition+"#"+childPosition);
    vh.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkBox= (CheckBox) v;
            String tag= (String) checkBox.getTag();
            int gpostion=Integer.parseInt(tag.split("#")[0]);
            int cpostion=Integer.parseInt(tag.split("#")[1]);
            ShopBean.DataBean dataBean = arr.getData().get(gpostion);
            ShopBean.DataBean.ListBean listBean = arr.getData().get(gpostion).getList().get(cpostion);
            listBean.setCheck_Child(checkBox.isChecked());
            double price = listBean.getPrice();
            if (checkBox.isChecked()){
                priceAll+=price;
            }else {
                priceAll-=price;
            }
            mTextView.setText(priceAll+"");

            dataBean.setCheck_group(checkBox.isChecked());
            mcCheckBox.setChecked(isSelectAllGroup());
            notifyDataSetChanged();
        }

       
    });
    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}
class ViewHolder{
    CheckBox checkBox;
    TextView name;
}
class ViewHolder2{
    CheckBox checkBox;
    ImageView imageView;
    TextView name,price;
}
//选中全部组
private boolean isSelectAllGroup() {
    for (int i = 0; i < arr.getData().size(); i++) {
        if (!(arr.getData().get(i).isCheck_group())){
            return false;
        }
    }
    return true;
}
//选中组
private void SelectGroup(int groupPostion,boolean checked) {
    for (int i = 0; i < arr.getData().get(i).getList().size(); i++) {
        ShopBean.DataBean.ListBean listBean = arr.getData().get(groupPostion).getList().get(i);
        listBean.setCheck_Child(checked);
    }
}

}
//mainActivity
public class Show_Frag extends BaseFragment implements IMainView {

private ExpandableListView lv;
private ShopPresenter shopPresenter;
private ShopAdapter adapter;
private CheckBox checkBox;
private TextView price;

@Override
protected void initData() {
    //绑定P层 接收数据
    shopPresenter = new ShopPresenter();
    shopPresenter.setView(this);
    shopPresenter.loadDataNet();

    //适配器
    adapter = new ShopAdapter(getActivity());
    lv.setAdapter(adapter);
    adapter.setMcCheckBox(checkBox);
    adapter.setmTextView(price);
    adapter.notifyDataSetChanged();

}

@Override
protected void initView() {
    lv = getActivity().findViewById(R.id.list);
    checkBox = getActivity().findViewById(R.id.checkbox_show);
    price = getView().findViewById(R.id.price_show);
}

@Override
protected int bindView() {
    return R.layout.show_frag;
}

@Override
public void onSuccess(ShopBean shopBean) {
    adapter.setArr(shopBean);
}

@Override
public void onErr(String errMsg) {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值