购物车的逻辑代码与适配器


Bean里添加的状态

            private int isFrist;   //是否选中商家
            public boolean shopSelected;  //商家多选是否选中
            public boolean itemSelected;   //条目多选是否选中


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.IViewHolder>{
    Context context;
    private List<String> ImageList = new ArrayList<>();
    private List<GetBean.DataBean.ListBean> list;
    Map<String,String> map = new Hashtable<>();
    public MyAdapter(Context context) {
        this.context = context;
    }


    public void add(GetBean getBean){
        if(list==null){
            list = new ArrayList<>();
        }
        for(GetBean.DataBean shop:getBean.getData()){
            map.put(shop.getSellerid(),shop.getSellerName());
            for(int i = 0; i<shop.getList().size();i++){
                list.add(shop.getList().get(i));
            }
        }

        setFirst(list);
        notifyDataSetChanged();

    }

    public void setFirst(List<GetBean.DataBean.ListBean> list){
        if(list.size()>0){
            list.get(0).setIsFrist(1);
            for(int i = 1;i<list.size();i++){
                if(list.get(i).getSellerid()==list.get(i-1).getSellerid()){
                    list.get(i).setIsFrist(2);
                }
                else{
                    list.get(i).setIsFrist(1);
                }
            }
        }
    }

    @Override
    public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.layout_adapter, null);
        return new IViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final IViewHolder holder, final int position) {

        if(list.get(position).getIsFrist() == 1){
            //显示商家
            holder.shop_check.setVisibility(View.VISIBLE);
            holder.shop_name.setVisibility(View.VISIBLE);
            holder.shop_check.setChecked(list.get(position).isShopSelected());

//            显示商家的名称
//            list.get(position).getSellerid() 取到商家的id
//            map.get()取到 商家的名称
            holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));
        } else {
            holder.shop_check.setVisibility(View.GONE);
            holder.shop_name.setVisibility(View.GONE);
        }

        holder.shoptitle.setText(list.get(position).getTitle());
        holder.shopprice.setText(list.get(position).getPrice()+"");
        holder.checkbox.setChecked(list.get(position).isItemSelected());
        String images = list.get(position).getImages();
        String[] split = images.split("\\|");
        for(int i = 0;i<split.length;i++){
            ImageList.add(split[i]);
        }
        ImageLoader.getInstance().displayImage(ImageList.get(position),holder.shopface);

        holder.plusView.setEditView(list.get(position).getNum());
        holder.shopdel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                setFirst(list);
                notifyDataSetChanged();
                sum(list);
            }
        });

        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).setItemSelected(holder.checkbox.isChecked());

                for(int i = 0;i<list.size();i++){
                    for(int j = 0;j<list.size();j++){
                        if(list.get(i).getSellerid() == list.get(j).getSellerid() && !list.get(j).isItemSelected()){
                            list.get(i).setShopSelected(false);
                            break;
                        }
                        else{
                            list.get(i).setShopSelected(true);
                        }
                    }
                }

                notifyDataSetChanged();
                sum(list);
            }
        });

        holder.plusView.setLinsenter(new PlusView.ClickLisenter() {
            @Override
            public void click(int count) {
                list.get(position).setNum(count);
                notifyDataSetChanged();
                sum(list);
            }
        });

        holder.shop_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).setShopSelected(holder.shop_check.isChecked());
                for(int i=0; i<list.size();i++){
                    if(list.get(position).getSellerid()==list.get(i).getSellerid()){
                            list.get(i).setItemSelected(holder.shop_check.isChecked());
                    }
                }

                notifyDataSetChanged();
                sum(list);
            }
        });
    }

    public void sum(List<GetBean.DataBean.ListBean> list){
        int totalNum = 0;
        float totalMoney = 0.0f;
        boolean allcheck = true;
        for(int i = 0; i <list.size(); i++){
            if(list.get(i).isItemSelected()){
                totalNum += list.get(i).getNum();
                totalMoney += list.get(i).getPrice()*list.get(i).getNum();
            }
            else{
                allcheck = false;
            }
        }

        listener.setTotal(totalMoney+"",totalNum+"",allcheck);
    }


    public void selectAll(boolean check){
        for(int i = 0;i<list.size();i++){
            list.get(i).setShopSelected(check);
            list.get(i).setItemSelected(check);
        }

        notifyDataSetChanged();
        sum(list);
    }

    @Override
    public int getItemCount() {

        return list==null ? 0 : list.size();
    }

    static class IViewHolder extends RecyclerView.ViewHolder{

        private final PlusView plusView;
        private final Button shopdel;
        private final CheckBox shop_check;
        private final ImageView shopface;
        private final TextView shop_name;
        private final CheckBox checkbox;
        private final TextView shoptitle;
        private final TextView shopprice;

        public IViewHolder(View itemView) {
            super(itemView);

            plusView = (PlusView) itemView.findViewById(R.id.plusView);
            shopdel = (Button) itemView.findViewById(R.id.shopdel);
            shop_check = (CheckBox) itemView.findViewById(R.id.shop_check);
            shopface = (ImageView) itemView.findViewById(R.id.shopface);
            shop_name = (TextView) itemView.findViewById(R.id.shop_name);
            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            shoptitle = (TextView) itemView.findViewById(R.id.shoptitle);
            shopprice = (TextView) itemView.findViewById(R.id.shopprice);
        }
    }
    UpdateUiListener listener;
    public void setListener(UpdateUiListener listener){
        this.listener = listener;
    }

    public interface UpdateUiListener{
        public void setTotal(String total,String num,boolean allcheck);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值