购物车

ICallBack

public interface ICallBack {
    void success(Object o);

    void failed(Exception e);
}

view

public interface IView {

    void success(MessageBean<List<shopper<List<product>>>> data);

    void failed(Exception e);
}

model

public class CartModel {
    public void getData(String url, ICallBack callBack, Type type){
        HttpUtils.getInstance().get(url,callBack,type);
    }
}

presenter

public class CartPresenter {
    private IView iv;
    private CartModel  model;
    public void attach(IView iv){
        this.iv = iv;
        model = new CartModel();
    }
    public void detach(){
        if (iv != null){
            iv = null;
        }
    }
    public void getData(){
        String url = "http://www.zhaoapi.cn/product/getCarts?uid=1538";
        Type type = new TypeToken<MessageBean<List<shopper<List<product>>>>>(){}.getType();
        model.getData(url, new ICallBack() {
            @Override
            public void success(Object o) {
                MessageBean<List<shopper<List<product>>>> data = (MessageBean<List<shopper<List<product>>>>) o;
                iv.success(data);
            }

            @Override
            public void failed(Exception e) {
                iv.failed(e);
            }
        },type);
    }
}

mainactivity

public class MainActivity extends AppCompatActivity implements IView {

    private RecyclerView rcContent;
    private CheckBox cbTotalSelect;
    private List<shopper<List<product>>> list;
    private ShopperAdapter adapter;
    private CartPresenter presenter;
    private TextView txtPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        setListener();
    }

    private void setListener() {

        cbTotalSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = cbTotalSelect.isChecked();
                //遍历商家列表 和下方的全选状态一致
                for (shopper<List<product>> shopper : list) {
                    shopper.setChecked(isChecked);
                    List<product> products = shopper.getList();
                    for (product product : products) {
                        product.setChecked(isChecked);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        //商家的adapter
        adapter = new ShopperAdapter(this, list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rcContent.setLayoutManager(layoutManager);
        rcContent.setAdapter(adapter);
        //添加商家状态发生变化时的监听
        adapter.setOnShopperClickListener(new ShopperAdapter.OnShopperClickListener() {
            @Override
            public void onShopperClick(int position, boolean isChecked) {
                //如果商家不选中,全选按钮也是未选中
                if (!isChecked) {
                    cbTotalSelect.setChecked(false);
                } else {
                    //遍历商家
                    boolean isAllshopperChecked = true;
                    for (shopper<List<product>> shopper : list) {
                        if (!shopper.isChecked()){
                            isAllshopperChecked = false;
                            break;
                        }
                    }
                    cbTotalSelect.setChecked(isAllshopperChecked);
                }
                calulatePrice();
            }
        });
        adapter.setOnAddDeacreaseProductListener(new ProductAdapter.OnAddDeacreaseProductListener() {
            @Override
            public void onChange(int position, int num) {
                calulatePrice();
            }
        });
        presenter = new CartPresenter();
        presenter.attach(this);
        presenter.getData();
    }

    private void initView() {
        rcContent = findViewById(R.id.rc_content);
        cbTotalSelect = findViewById(R.id.cb_total_select);
        txtPrice = findViewById(R.id.txt_price);
    }
    private void calulatePrice(){
        float totalPrice = 0;
        for (shopper<List<product>> shopper : list) {
            for (product product : shopper.getList()) {
                if (product.isChecked()){
                    totalPrice += product.getNum()*product.getPrice();
                }
            }
        }
        txtPrice.setText("总价:" + totalPrice);
    }

    @Override
    public void success(MessageBean<List<shopper<List<product>>>> data) {
        if (data != null) {
            List<shopper<List<product>>> shoppers = data.getData();
            if (shoppers != null) {
                list.clear();
                list.addAll(shoppers);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void failed(Exception e) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detach();
        }
    }
}

mainactivity_xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.windows.shoppingcart.MainActivity">

    <RelativeLayout
        android:id="@+id/cart_title"
        android:padding="10dp"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txt_bus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="购物车"/>
        <TextView
            android:id="@+id/txt_bianji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/cart_bottom"
        android:padding="10dp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/cb_total_select"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>

        <TextView
            android:id="@+id/txt_price"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="价格"/>
        <Button
            android:id="@+id/btn_calu"
            android:text="结算"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rc_content"
        android:layout_below="@id/cart_title"
        android:layout_above="@id/cart_bottom"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

商家adapter

public class ShopperAdapter extends RecyclerView.Adapter<ShopperAdapter.ViewHolder> {
    private Context context;
    private List<shopper<List<product>>> list;

    public ShopperAdapter(Context context, List<shopper<List<product>>> list) {
        this.context = context;
        this.list = list;
    }

    //商家点击时间 (商家发生变化的接口)
    public interface OnShopperClickListener{
        void onShopperClick(int position,boolean isChecked);
    }
    private OnShopperClickListener shopperClickListener;
    public void setOnShopperClickListener(OnShopperClickListener listener){
        this.shopperClickListener = listener;
    }
    //商品加减器的监听
    private ProductAdapter.OnAddDeacreaseProductListener productListener;
    public void setOnAddDeacreaseProductListener(ProductAdapter.OnAddDeacreaseProductListener listener){
        this.productListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final shopper<List<product>> shopper = list.get(position);
        holder.txtShopperName.setText(shopper.getSellerName());
        //布局管理器
        RecyclerView.LayoutManager pLayoutManager = new LinearLayoutManager(context);
        holder.rcProduct.setLayoutManager(pLayoutManager);
        final ProductAdapter adapter = new ProductAdapter(context,shopper.getList());
        //商品加减器的监听器
        if (productListener != null){
            adapter.setOnAddDeacreaseProductListener(productListener);
        }
        //商品复选框点击事件
        adapter.setOnProuductClickListener(new ProductAdapter.OnProuductClickListener() {
            @Override
            public void onProductClick(int position, boolean isChecked) {
                //如果当前商品没有选中
                if (!isChecked){
                    //商家也是未选中
                    shopper.setChecked(false);
                    shopperClickListener.onShopperClick(position,false);
                }else {
                    boolean isAllProductSelected = true;
                    for (product product : shopper.getList()) {
                        if (!product.isChecked()){
                            isAllProductSelected = false;
                        }
                    }
                    shopper.setChecked(isAllProductSelected);

                    shopperClickListener.onShopperClick(position,true);
                }
                notifyDataSetChanged();
                productListener.onChange(0,0);
            }
        });
        holder.rcProduct.setAdapter(adapter);
        holder.cbShopper.setOnCheckedChangeListener(null);
        holder.cbShopper.setChecked(shopper.isChecked());
        holder.cbShopper.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                List<product> productList = shopper.getList();
                for (product product : productList) {
                    product.setChecked(isChecked);
                }
                adapter.notifyDataSetChanged();
                if (shopperClickListener != null){
                    shopperClickListener.onShopperClick(position,isChecked);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private CheckBox cbShopper;//按钮
        private TextView txtShopperName;
        private RecyclerView rcProduct;
        public ViewHolder(View itemView) {
            super(itemView);
            cbShopper = itemView.findViewById(R.id.cb_shopper);
            txtShopperName = itemView.findViewById(R.id.txt_shopper_name);
            rcProduct = itemView.findViewById(R.id.rc_product);
        }
    }
}

商品adapter

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{
    private Context context;
    private List<product> list;

    public ProductAdapter(Context context, List<product> list) {
        this.context = context;
        this.list = list;
    }

    //商品的点击时间监听
    public interface OnProuductClickListener{
        void onProductClick(int position,boolean isChecked);
    }
    private OnProuductClickListener prouductClickListener;
    public void setOnProuductClickListener(OnProuductClickListener listener){
        this.prouductClickListener = listener;
    }
    //加减器发生变化监听
    public interface OnAddDeacreaseProductListener{
        void onChange(int position, int num);
    }
    private OnAddDeacreaseProductListener listener;
    public void setOnAddDeacreaseProductListener(OnAddDeacreaseProductListener listener){
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        //展示商品的信息
        final product product = list.get(position);
        String images = product.getImages();
        if (!TextUtils.isEmpty(images)){
            String [] strings = images.split("\\|");
            if (strings.length > 0){
                Glide.with(context).load(StringUtils.https2Http(strings[0])).into(holder.imageProduct);
            }
        }
        holder.txtProductName.setText(product.getTitle());
        holder.txtSinglePrice.setText(String.valueOf(product.getPrice()));
        holder.advProduct.setNum(product.getNum());
        //加减器添加点击事件
        holder.advProduct.setOnAddDecreaseClickListener(new AddDecreaseView.OnAddDecreaseClickListener() {
            @Override
            public void add(int num) {
                product.setNum(num);
                if (listener != null){
                    listener.onChange(position,num);
                }
            }

            @Override
            public void decrease(int num) {
                product.setNum(num);
                listener.onChange(position,num);
            }
        });
        //商品的复选框
        holder.cbProduct.setOnCheckedChangeListener(null);
        holder.cbProduct.setChecked(product.isChecked());
        holder.cbProduct.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                product.setChecked(isChecked);
                if (prouductClickListener != null){
                    prouductClickListener.onProductClick(position,isChecked);
                }
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private CheckBox cbProduct;
        private ImageView imageProduct;
        private TextView txtProductName;
        private TextView txtSinglePrice;
        private AddDecreaseView advProduct;
        public ViewHolder(View itemView) {
            super(itemView);
            cbProduct = itemView.findViewById(R.id.cb_product);
            imageProduct = itemView.findViewById(R.id.iamge_product);
            txtProductName = itemView.findViewById(R.id.txt_product_name);
            txtSinglePrice = itemView.findViewById(R.id.txt_single_price);
            advProduct = itemView.findViewById(R.id.adv_product);
        }
    }
}

商家对应的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:padding="5dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb_shopper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/txt_shopper_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rc_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

商品对应的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="5dp"
    android:paddingLeft="20dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp">

    <CheckBox
        android:id="@+id/cb_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iamge_product"
        android:layout_toRightOf="@id/cb_product"
        android:layout_width="45dp"
        android:layout_height="45dp" />

    <com.example.windows.shoppingcart.widget.AddDecreaseView
        android:id="@+id/adv_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"></com.example.windows.shoppingcart.widget.AddDecreaseView>
    <LinearLayout
        android:orientation="vertical"
        android:layout_toLeftOf="@id/adv_product"
        android:layout_toRightOf="@id/iamge_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txt_product_name"
            android:layout_toRightOf="@id/iamge_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"/>
        <TextView
            android:id="@+id/txt_single_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

MessageBena

private String msg;
private String code;
private T data;

Shopper bean

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

自定义加减器

public class AddDecreaseView extends RelativeLayout implements View.OnClickListener {
    private TextView txtAdd;
    private TextView txtDecrease;
    private TextView txtNum;
    private int num;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.txt_add:
                num++;
                txtNum.setText(num + "");
                if (listener != null){
                    listener.add(num);
                }
                break;
            case R.id.txt_decrease:
                if (num>1){
                    num--;
                }
                txtNum.setText(num + "");
                if (listener != null){
                    listener.decrease(num);
                }
                break;
        }
    }

    public interface OnAddDecreaseClickListener{
        void add(int num);

        void decrease(int num);
    }
    private OnAddDecreaseClickListener listener;
    public void setOnAddDecreaseClickListener(OnAddDecreaseClickListener listener){
        this.listener = listener;
    }
    public AddDecreaseView(Context context) {
        this(context,null);
    }

    public AddDecreaseView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public AddDecreaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View.inflate(context, R.layout.layout_add_decrease_view,this);
        txtAdd = findViewById(R.id.txt_add);
        txtDecrease = findViewById(R.id.txt_decrease);
        txtNum = findViewById(R.id.txt_num);

        txtNum.setText(" 1 ");

        txtAdd.setOnClickListener(this);
        txtDecrease.setOnClickListener(this);
    }
    public void setNum(int num){
        this.num = num;
        txtNum.setText(num + "");
    }
    public int getNum(){
        return num;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值