美文网首页
购物车实现

购物车实现

作者: 巡皇wx | 来源:发表于2017-10-24 18:21 被阅读0次

    自定义二级列表

    public class MyExpandableListView extends ExpandableListView{

    public MyExpandableListView(Context context) {

    super(context);

    }

    public MyExpandableListView(Context context, AttributeSet attrs) {

    super(context, attrs);

    }

    public MyExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {

    super(context, attrs, defStyleAttr);

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    // TODO Auto-generated method stub

    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,

    MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec, expandSpec);

    }

    }

    二级列表适配器

    public class Adapter_Cart extends BaseExpandableListAdapter {

    private Context context;

    private CartBean cartBean;

    private sendFlag sendFlag;

    public Adapter_Cart(Context context, CartBean cartBean, Adapter_Cart.sendFlag sendFlag) {

    this.context = context;

    this.cartBean = cartBean;

    this.sendFlag = sendFlag;

    }

    @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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    final GroupHolder holder;

    if (convertView == null) {

    //获取组布局

    convertView = View.inflate(context, R.layout.group_item, null);

    //实例GroupHolder

    holder = new GroupHolder();

    //获取控件

    holder.groupCheck = (CheckBox) convertView.findViewById(R.id.group_check);

    holder.groupName = (TextView) convertView.findViewById(R.id.group_name);

    convertView.setTag(holder);

    } else {

    holder = (GroupHolder) convertView.getTag();

    }

    //赋值

    holder.groupName.setText(cartBean.getData().get(groupPosition).getSellerName());

    holder.groupCheck.setChecked(cartBean.getData().get(groupPosition).isSischecked());

    //全选点击事件

    holder.groupCheck.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    //得到状态

    boolean checked = holder.groupCheck.isChecked();

    //赋值

    cartBean.getData().get(groupPosition).setSischecked(checked);

    //店铺是否选择 此店铺所有商品也跟着是否选择

    for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) {

    cartBean.getData().get(groupPosition).getList().get(i).setIschecked(checked);

    }

    //刷新适配器

    notifyDataSetChanged();

    //总价格 总数量 是否全选

    double t = 0;

    int count = 0;

    boolean flag = true;

    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).ischecked()){

    t+=cartBean.getData().get(i).getList().get(j).getPrice()*cartBean.getData().get(i).getList().get(j).getNum();

    count+=cartBean.getData().get(i).getList().get(j).getNum();

    }

    }

    if (!cartBean.getData().get(i).isSischecked()){

    flag = false;

    }

    }

    //接口传值

    sendFlag.sendFlags(flag);

    sendFlag.sendTotalAndCounts(t,count);

    }

    });

    return convertView;

    }

    @Override

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final ChildHolder holder;

    if (convertView == null) {

    //获取组布局

    convertView = View.inflate(context, R.layout.child_item, null);

    //实例GroupHolder

    holder = new ChildHolder();

    //获取控件

    holder.childCheck = (CheckBox) convertView.findViewById(R.id.child_check);

    holder.childTitle = (TextView) convertView.findViewById(R.id.child_title);

    holder.childPrice = (TextView) convertView.findViewById(R.id.child_price);

    holder.childSubhead = (TextView) convertView.findViewById(R.id.child_subhead);

    holder.childPic = (ImageView) convertView.findViewById(R.id.child_pic);

    holder.addsub = (NumberAddSubView) convertView.findViewById(R.id.addsub);

    convertView.setTag(holder);

    } else {

    holder = (ChildHolder) convertView.getTag();

    }

    holder.addsub.setMaxValue(Integer.MAX_VALUE);

    String[] strings = cartBean.getData().get(groupPosition).getList().get(childPosition).getImages().split("\\|");

    Glide.with(context).load(strings[0]).into(holder.childPic);

    holder.childCheck.setChecked(cartBean.getData().get(groupPosition).getList().get(childPosition).ischecked());

    holder.childTitle.setText(cartBean.getData().get(groupPosition).getList().get(childPosition).getTitle());

    holder.childSubhead.setText(cartBean.getData().get(groupPosition).getList().get(childPosition).getSubhead());

    holder.childPrice.setText(cartBean.getData().get(groupPosition).getList().get(childPosition).getPrice()+"");

    holder.addsub.setValue(cartBean.getData().get(groupPosition).getList().get(childPosition).getNum());

    //复选框点击监听

    holder.childCheck.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    boolean checked = holder.childCheck.isChecked();

    //赋值

    cartBean.getData().get(groupPosition).getList().get(childPosition).setIschecked(checked);

    //确定店铺状态

    boolean s_flag = true;

    for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) {

    if (!cartBean.getData().get(groupPosition).getList().get(i).ischecked()){

    s_flag = false;

    }

    }

    cartBean.getData().get(groupPosition).setSischecked(s_flag);

    //刷新适配器

    notifyDataSetChanged();

    //总价格 总数量 是否全选

    double t = 0;

    int count = 0;

    boolean flag = true;

    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).ischecked()){

    t+=cartBean.getData().get(i).getList().get(j).getPrice()*cartBean.getData().get(i).getList().get(j).getNum();

    count+=cartBean.getData().get(i).getList().get(j).getNum();

    }

    }

    if (!cartBean.getData().get(i).isSischecked()){

    flag = false;

    }

    }

    //接口传值

    sendFlag.sendFlags(flag);

    sendFlag.sendTotalAndCounts(t,count);

    }

    });

    return convertView;

    }

    @Override

    public boolean isChildSelectable(int groupPosition, int childPosition) {

    return true;

    }

    class GroupHolder {

    private CheckBox groupCheck;

    private TextView groupName;

    }

    class ChildHolder {

    private CheckBox childCheck;

    private TextView childTitle;

    private TextView childSubhead;

    private TextView childPrice;

    private ImageView childPic;

    private NumberAddSubView addsub;

    }

    public interface sendFlag {

    void sendFlags(boolean flag);

    void sendTotalAndCounts(double t, int count);

    }

    }

    相关文章

      网友评论

          本文标题:购物车实现

          本文链接:https://www.haomeiwen.com/subject/pvyxpxtx.html