二级列表实现购物车

效果图

Activity页面

public class MainActivity extends AppCompatActivity implements MyInterFace<Recult>, MyAdapter.TotalPriceListener {

    private MyAdapter myAdapter;
    private Presenter presenter;
    private ExpandableListView expandableListView;
    private TextView zj;
    private CheckBox qx;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = findViewById(R.id.elv);
        //设置父级不可合并
        expandableListView.setGroupIndicator(null);
        
        qx = findViewById(R.id.qx);
        zj = findViewById(R.id.zj);
        button = findViewById(R.id.jiesuan);
        //p层
        presenter = new Presenter(this);

        //添加适配器
        myAdapter = new MyAdapter(this);

        expandableListView.setAdapter(myAdapter);

        //调用p层
        presenter.show();

        //全选复选框
        qx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                myAdapter.checkAll(isChecked);
            }
        });

    }

    //网络请求成功
    @Override
    public void success(Recult data) {
        
        List<Shop> data1 = (List<Shop>) data.getData();
        //向adapter传值
        myAdapter.addAll(data1);
        
        int groupCount = data1.size();
        //默认全部展开
        for (int i = 0; i < groupCount; i++) {
            expandableListView.expandGroup(i);
        }
        myAdapter.notifyDataSetChanged();
    }

    //网络请求失败
    @Override
    public void fail(Recult recult) {
        Toast.makeText(this, "" + recult.getMsg(), Toast.LENGTH_SHORT).show();
    }

    //解除绑定,避免内存泄露
    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unBind();
    }

    //总价
    @Override
    public void totalPrice(double totalPrice) {
        zj.setText(String.valueOf(totalPrice));
    }

    //总数
    @Override
    public void totalNum(int num) {
        button.setText(num + "");
    }

}

p层

public abstract class MyPresenter{
    MyInterFace myInterFace;

    public MyPresenter(MyInterFace myInterFace) {
        this.myInterFace = myInterFace;
    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Recult recult = (Recult) msg.obj;
            if (recult.getCode() == 0){
                myInterFace.success(recult);
            }else{
                myInterFace.fail(recult);
            }
        }
    };

    public void show(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                Message message = handler.obtainMessage();
                message.obj = getData();
                handler.sendMessage(message);
            }
        }.start();
    }

    public abstract Recult getData();
    //避免内存泄露
    public void unBind(){
        this.myInterFace = null;
    }
}
public class Presenter extends MyPresenter{
    public Presenter(MyInterFace myInterFace) {
        super(myInterFace);
    }

    @Override
    public Recult getData() {
        MyModel myModel = new MyModel();
        Recult recult = myModel.show();
        return recult;
    }
}

m层

Gson gson = new Gson();
            Type type = new TypeToken<Recult<List<Shop>>>() {
            }.getType();
            Recult recult = gson.fromJson(string, type);
            return recult;

adapter页面

public class MyAdapter extends BaseExpandableListAdapter {

    List<Shop> list = new ArrayList<>();

    TotalPriceListener totalPriceListener;

    public MyAdapter(TotalPriceListener totalPriceListener) {
        this.totalPriceListener = totalPriceListener;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.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 false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = View.inflate(parent.getContext(), R.layout.item_grioup, null);
        CheckBox checkBox = convertView.findViewById(R.id.dp);
        final Shop shop = list.get(groupPosition);
        checkBox.setText(shop.getSellerName());
        //设置店铺选中状态
        checkBox.setChecked(shop.isCheck());

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //数据更新
                shop.setCheck(isChecked);
                //得到商品信息
                List<Bean> list1 = list.get(groupPosition).getList();
                //商品信息循环赋值
                for (int i = 0; i < list1.size(); i++) {
                    //店铺选中商品必须选中
                    list1.get(i).setSelected(isChecked ? 1 : 0);
                }
                notifyDataSetChanged();
                total();
                zs();
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
        convertView = View.inflate(parent.getContext(), R.layout.item_child, null);
        CheckBox checkBox = convertView.findViewById(R.id.cb);
        ImageView imageView = convertView.findViewById(R.id.iv);
        TextView textView = convertView.findViewById(R.id.title);
        TextView textView1 = convertView.findViewById(R.id.price);
        Button button = convertView.findViewById(R.id.jia);
        Button button1 = convertView.findViewById(R.id.jian);
        final EditText editText = convertView.findViewById(R.id.sum);

        Bean bean = list.get(groupPosition).getList().get(childPosition);
        String images = bean.getImages();
        String[] strings = images.split("!");
        Glide.with(parent.getContext()).load(strings[0]).into(imageView);
        textView.setText(bean.getTitle());
        textView1.setText("¥:" + bean.getPrice());
        editText.setText(bean.getNum() + "");
        //商品选中
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    list.get(groupPosition).getList().get(childPosition).setSelected(isChecked ? 1 : 0);
                } else {
                    list.get(groupPosition).getList().get(childPosition).setSelected(isChecked ? 1 : 0);

                }
                notifyDataSetChanged();
                total();
                zs();
            }
        });

        //店铺选中商品选中
        if (bean.getSelected() == 0) {
            checkBox.setChecked(false);
        } else {
            checkBox.setChecked(true);
        }

        //设置商品数量
        //加加
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = Integer.parseInt(editText.getText().toString());
                i++;
                list.get(groupPosition).getList().get(childPosition).setNum(i);
//                editText.setText(i + "");
                notifyDataSetChanged();
            }
        });
        //减减
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = Integer.parseInt(editText.getText().toString());
                i--;
                if (i < 1) {
                    Toast.makeText(parent.getContext(), "商品数量不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                list.get(groupPosition).getList().get(childPosition).setNum(i);
                notifyDataSetChanged();
            }
        });


        return convertView;
    }

    /**
     * 全选
     */
    public void checkAll(boolean isCheck) {
        //循环商家
        for (int i = 0; i < list.size(); i++) {
            Shop shop = list.get(i);
            shop.setCheck(isCheck);
            for (int j = 0; j < shop.getList().size(); j++) {
                Bean bean = shop.getList().get(j);
                //判断是否选中
                bean.setSelected(isCheck ? 1 : 0);
            }
            notifyDataSetChanged();
            total();
            zs();
        }
    }

    /**
     * 计算价格
     */
    public void total() {
        double totalPrice = 0;
        for (int i = 0; i < list.size(); i++) {
            Shop shop = list.get(i);
            for (int j = 0; j < shop.getList().size(); j++) {
                Bean bean = shop.getList().get(j);
                if (bean.getSelected() == 1) {
                    totalPrice = totalPrice + bean.getPrice() * bean.getNum();
                }
            }
        }
        //将价格传入接口中
        if (totalPriceListener != null) {
            totalPriceListener.totalPrice(totalPrice);
        }
    }

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

    public void addAll(List<Shop> data1) {
        if (data1 != null) {
            list.addAll(data1);
        }
    }
    //计算数量
    public void zs() {
        int a = 0;
        for (int i = 0; i < list.size(); i++) {//循环的商家
            Shop shop = list.get(i);
            for (int j = 0; j < shop.getList().size(); j++) {
                Bean bean = shop.getList().get(j);
                if (bean.getSelected() == 1) {//如果是选中状态
                    a = a + bean.getNum();
                }
            }
        }
        //将数量传入接口中
        if (totalPriceListener != null) {
            totalPriceListener.totalNum(a);
        }
    }
    //接口回调 总价和总数
    public interface TotalPriceListener {
        void totalPrice(double totalPrice);
        void totalNum(int num);
    }
}

bean类

店铺类

public class Shop {
    List<Bean> list;
    String sellerName;
    String sellerid;
    boolean check;

    public void setCheck(boolean check) {
        this.check = check;
    }

    public boolean isCheck() {

        return check;
    }

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

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

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

    public List<Bean> getList() {

        return list;
    }

    public String getSellerName() {
        return sellerName;
    }

    public String getSellerid() {
        return sellerid;
    }
}

商品类

public class Bean {
    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 int count = 1;

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

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

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

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

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

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

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

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

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

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

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

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

    public void setCount(int count) {
        this.count = count;
    }

    public double getBargainPrice() {

        return bargainPrice;
    }

    public String getCreatetime() {
        return createtime;
    }

    public String getDetailUrl() {
        return detailUrl;
    }

    public String getImages() {
        return images;
    }

    public int getNum() {
        return num;
    }

    public int getPid() {
        return pid;
    }

    public double getPrice() {
        return price;
    }

    public int getPscid() {
        return pscid;
    }

    public int getSelected() {
        return selected;
    }

    public int getSellerid() {
        return sellerid;
    }

    public String getSubhead() {
        return subhead;
    }

    public String getTitle() {
        return title;
    }

    public int getCount() {
        return count;
    }
}
public class Recult<T> {
    int code;
    String msg;
    T data;

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

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

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

    public int getCode() {

        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值