Android仿淘宝购物车的用ExpandableListView实现

效果图
Android仿淘宝购物车的用ExpandableListView实现_第1张图片

主布局




    

    

    

    

        

        

        

        
    


父条目布局




    

    

子条目布局




    

        

        

        

            

            

                

                


                

            

        

    

自定义布局




    

    

    


自定义代码

public class AddAndSub extends LinearLayout {

    private View rootview;
    private TextView sub;
    private TextView number;
    private TextView add;

    public AddAndSub(Context context) {
        this(context, null);
    }

    public AddAndSub(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public AddAndSub(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
        listener();
    }

    private void listener() {
        //减
        sub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sub();
            }
        });

        //加
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
    }


    private void sub() {
        String num = number.getText().toString().trim();
        int numb = Integer.parseInt(num);
        if (numb > 1) {
            numb--;
            setCurrentNum(numb);
        } else {
            Toast.makeText(getContext(), "用户最小数量为1", Toast.LENGTH_SHORT).show();
        }
    }

    private void add() {
        String num = number.getText().toString().trim();
        int numb = Integer.parseInt(num);
        numb++;
        setCurrentNum(numb);
    }

    private void initview(Context context) {
        rootview = View.inflate(context, R.layout.addandsum, this);
        sub = rootview.findViewById(R.id.num_sub);
        number = rootview.findViewById(R.id.number);
        add = rootview.findViewById(R.id.num_add);
        /*number.setText("1");*/
    }

    //设置获取当前数量的方法
    public void setCurrentNum(int num) {
        number.setText("" + num);
        if (onChangNum != null) {
            onChangNum.changeNum(this, num);
        }
    }

    public interface OnChangNum {
        void changeNum(View view, int currentNum);
    }

    private OnChangNum onChangNum;

    public void setOnChangNumListener(OnChangNum onChangNum) {
        this.onChangNum = onChangNum;
    }
}

bean类

public class ShopEntity {

    private String msg;
    private String code;
    private List data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * list : []
         * sellerName :
         * sellerid : 0
         */

        private String sellerName;
        private String sellerid;
        private List list;
        private boolean isChecked;

        public boolean isChecked() {
            return isChecked;
        }

        public void setChecked(boolean checked) {
            isChecked = checked;
        }

        public String getSellerName() {
            return sellerName;
        }

        public void setSellerName(String sellerName) {
            this.sellerName = sellerName;
        }

        public String getSellerid() {
            return sellerid;
        }

        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }

        public List getList() {
            return list;
        }

        public void setList(List list) {
            this.list = list;
        }


        public static class ListBean {

            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private double price;
            private int pscid;
            private int selected;
            private int sellerid;
            private String subhead;
            private String title;
            private boolean isChecked;

            public boolean isChecked() {
                return selected != 0;
            }

            public void setChecked(boolean checked) {
                isChecked = checked;
            }

            public double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double bargainPrice) {
                this.bargainPrice = bargainPrice;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public String getDetailUrl() {
                return detailUrl;
            }

            public void setDetailUrl(String detailUrl) {
                this.detailUrl = detailUrl;
            }

            public String getImages() {
                return images;
            }

            public void setImages(String images) {
                this.images = images;
            }

            public int getNum() {
                return num;
            }

            public void setNum(int num) {
                this.num = num;
            }

            public int getPid() {
                return pid;
            }

            public void setPid(int pid) {
                this.pid = pid;
            }

            public double getPrice() {
                return price;
            }

            public void setPrice(int price) {
                this.price = price;
            }

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }

            public int getSelected() {
                return selected;
            }

            public void setSelected(int selected) {
                this.selected = selected;
            }

            public int getSellerid() {
                return sellerid;
            }

            public void setSellerid(int sellerid) {
                this.sellerid = sellerid;
            }

            public String getSubhead() {
                return subhead;
            }

            public void setSubhead(String subhead) {
                this.subhead = subhead;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }
        }
    }
}

在activity中

public class MainActivity extends AppCompatActivity implements ShopView {

    @BindView(R.id.elv)
    ExpandableListView elv;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.sum_price)
    TextView sumPrice;
    @BindView(R.id.js)
    TextView js;
    private ShopPresenter shopPresenter;
    private ShopAdapter shopAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        shopPresenter = new ShopPresenter(this);
        shopPresenter.showShop();

        //全选
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setcheckAll(1);
                } else {
                    setcheckAll(0);
                }
            }
        });

        //跳转到订单页面
        js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, OrderActivity.class));
            }
        });
    }

    //全选
    private void setcheckAll(int s) {
        int groupCount = shopAdapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            ShopEntity.DataBean group = (ShopEntity.DataBean) shopAdapter.getGroup(i);
            boolean checked = group.isChecked();
            if (!checked) {
                group.setChecked(true);
            } else {
                group.setChecked(false);
            }

            List list = group.getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(s);
            }
        }
        shopAdapter.notifyDataSetChanged();
        getTotal();
    }

    //计算总价
    public void getTotal() {
        double total = 0;
        List list = shopAdapter.getList();
        for (int i = 0; i < list.size(); i++) {
            List list1 = list.get(i).getList();
            for (int j = 0; j < list1.size(); j++) {
                boolean checked = list1.get(j).isChecked();
                if (checked) {
                    double price = list1.get(j).getPrice();
                    int num = list1.get(j).getNum();
                    total += price * num;
                }
            }
        }

        sumPrice.setText("合计:" + total);
    }

    @Override
    public void onSuccessShop(List result) {
        shopAdapter = new ShopAdapter(this, result);
        //调用
        initItemChange();
        elv.setAdapter(shopAdapter);
        for (int i = 0; i < result.size(); i++) {
            elv.expandGroup(i);
        }
    }

    //点击条目进行变化
    public void initItemChange() {
        //点击父条目
        elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                ShopEntity.DataBean group = (ShopEntity.DataBean) shopAdapter.getGroup(groupPosition);
                group.setChecked(!group.isChecked());
                int cb = 0;
                if (group.isChecked()) {
                    cb = 1;
                }
                List list = group.getList();
                for (int i = 0; i < list.size(); i++) {
                    ShopEntity.DataBean.ListBean listBean = list.get(i);
                    listBean.setSelected(cb);
                }
                shopAdapter.notifyDataSetChanged();
                getTotal();
                return true;
            }
        });


        //点击子条目
        elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ShopEntity.DataBean.ListBean child = (ShopEntity.DataBean.ListBean) shopAdapter.getChild(groupPosition, childPosition);
                boolean checked = child.isChecked();
                if (checked) {
                    child.setSelected(0);
                } else {
                    child.setSelected(1);
                }
                shopAdapter.notifyDataSetChanged();
                getTotal();
                return true;
            }
        });

        //点击加减器
        shopAdapter.setOnChagenum(new ShopAdapter.OnChagenum() {
            @Override
            public void change(View view, int num) {
                getTotal();
            }
        });
    }

    @Override
    public void onFailerShop(String msg) {
        Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        shopPresenter.destory();
    }
}

在adapter中

public class ShopAdapter extends BaseExpandableListAdapter {
    private Context mcontext;
    private List group;

    public ShopAdapter(Context mcontext, List group) {
        this.mcontext = mcontext;
        this.group = group;
    }

    public List getList(){
        return group;
    }
    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return group.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return group.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 isExpanded, View convertView, ViewGroup parent) {
        MyGroupViewHolder holder1 = null;
        if (convertView == null) {
            holder1 = new MyGroupViewHolder();
            convertView = View.inflate(mcontext, R.layout.list_group, null);
            holder1.group_checkBox = convertView.findViewById(R.id.group_checkBox);
            holder1.group_name = convertView.findViewById(R.id.group_name);
            convertView.setTag(holder1);
        } else {
            holder1 = (MyGroupViewHolder) convertView.getTag();
        }
        ShopEntity.DataBean dataBean = group.get(groupPosition);
        holder1.group_checkBox.setChecked(dataBean.isChecked());
        holder1.group_name.setText(dataBean.getSellerName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyChildViewHolder holder2 = null;
        if (convertView == null) {
            holder2 = new MyChildViewHolder();
            convertView = View.inflate(mcontext, R.layout.list_child_item, null);
            holder2.child_checkBox = convertView.findViewById(R.id.child_checkBox);
            holder2.child_iamge = convertView.findViewById(R.id.child_iamge);
            holder2.child_title = convertView.findViewById(R.id.child_title);
            holder2.child_price = convertView.findViewById(R.id.child_price);
            holder2.myAddSub = convertView.findViewById(R.id.myAddSub);
            convertView.setTag(holder2);

        } else {
            holder2 = (MyChildViewHolder) convertView.getTag();
        }
        final ShopEntity.DataBean.ListBean listBean = group.get(groupPosition).getList().get(childPosition);
        holder2.child_checkBox.setChecked(listBean.isChecked());
        Picasso.with(mcontext).load(listBean.getImages().split("\\|")[0]).into(holder2.child_iamge);
        holder2.child_title.setText(listBean.getTitle());
        holder2.child_price.setText(listBean.getPrice() + "");
        holder2.myAddSub.setCurrentNum(listBean.getNum());

        holder2.myAddSub.setOnChangNumListener(new AddAndSub.OnChangNum() {
            @Override
            public void changeNum(View view, int currentNum) {
                listBean.setNum(currentNum);
                onChagenum.change(view, currentNum);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class MyGroupViewHolder {
        CheckBox group_checkBox;
        TextView group_name;
    }

    class MyChildViewHolder {
        CheckBox child_checkBox;
        ImageView child_iamge;
        TextView child_title, child_price;
        AddAndSub myAddSub;
    }

    public interface OnChagenum {
        void change(View view, int num);
    }

    private OnChagenum onChagenum;

    public void setOnChagenum(OnChagenum onChagenum) {
        this.onChagenum = onChagenum;
    }
}

你可能感兴趣的:(Android仿淘宝购物车的用ExpandableListView实现)