购物车详解之activity和mvp

本文详细介绍了使用Android平台开发购物车应用的过程,包括界面布局、数据加载、商品选择及总价计算等功能实现。通过XRecyclerView进行商品列表展示,利用ShopPresenter进行数据请求与处理,实现了商品的分页加载、全选功能及总价显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package zjj.bwie.com.shopcar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import zjj.bwie.com.shopcar.adapter.ShopAdapter;
import zjj.bwie.com.shopcar.adapter.ShopCheckedListenter;
import zjj.bwie.com.shopcar.adapter.ShopsCheckedlistener;
import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.common.Api;
import zjj.bwie.com.shopcar.presenter.ShopPresenter;
import zjj.bwie.com.shopcar.view.IShopView;

public class MainActivity extends AppCompatActivity implements IShopView,ShopCheckedListenter {

    private XRecyclerView xrecycle_view;
    private CheckBox cbox_all;
    private TextView tv_money;
    private int page=1;
    private List<ShopBean.DataBean> list;
    private ShopAdapter shopAdapter;
    private ShopPresenter shopPresenter;

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

        initView();
        initData();

    }



    private void initView() {
        xrecycle_view = findViewById(R.id.xrecycle_view);
        xrecycle_view.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        cbox_all = findViewById(R.id.cbox_all);
        tv_money = findViewById(R.id.tv_money);

        xrecycle_view.setLoadingMoreEnabled(true);
        xrecycle_view.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                initData();
            }

            @Override
            public void onLoadMore() {
                page++;
                initData();

            }
        });

        cbox_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cbox_all.isChecked()){
                    if(list!=null && list.size()>0){
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(true);
                            for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                                list.get(i).getList().get(i1).setSelected(true);
                            }
                        }
                    }
                }else {
                    if(list!=null &&list.size()>0){
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(false);
                            for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                                list.get(i).getList().get(i1).setSelected(false);
                            }
                        }
                    }
                }
                shopAdapter.notifyDataSetChanged();

            }
        });

    }
    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("uid","17403");
        map.put("page",page+"");
        shopPresenter = new ShopPresenter(this);
        shopPresenter.getShops(map,Api.SHOP_SELECT);
    }

    @Override
    public void onsuccess(ShopBean shopBean) {
        if(shopBean!=null && shopBean.getData()!=null){
            if(page==1){
                list = shopBean.getData();
                Log.i("aaaaaaa",list+"");
                shopAdapter = new ShopAdapter(MainActivity.this, list);
                xrecycle_view.setAdapter(shopAdapter);

                xrecycle_view.refreshComplete();
            }else {
                if (shopAdapter!=null){
                    shopAdapter.addDataPage(list);
                }
                xrecycle_view.refreshComplete();
            }
            shopAdapter.setShopCheckedListenter(this);
        }
    }

    @Override
    public void onfaliure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    //全选按钮是否选中的回调
    @Override
    public void notifyCbox() {

        StringBuilder stringBuilder = new StringBuilder();
        if (shopAdapter != null) {
            for (int i = 0; i < shopAdapter.getList().size(); i++) {
                stringBuilder.append(shopAdapter.getList().get(i).isSelected());
                for (int i1 = 0; i1 < shopAdapter.getList().get(i).getList().size(); i1++) {
                    stringBuilder.append(shopAdapter.getList().get(i).getList().get(i1).isSelected());
                }
            }
        }

        if (stringBuilder.toString().contains("false")) {
            cbox_all.setChecked(false);
        } else {
            cbox_all.setChecked(true);
        }

        totalPrice();
    }

    private void totalPrice() {
        double shopsprice=0;
        for (int i = 0; i < shopAdapter.getList().size(); i++) {
            for (int i1 = 0; i1 < shopAdapter.getList().get(i).getList().size(); i1++) {
                //计算总价的关键代码
                if(shopAdapter.getList().get(i).getList().get(i1).isSelected()){
                    ShopBean.DataBean.ListBean listBean = shopAdapter.getList().get(i).getList().get(i1);
                    shopsprice+=listBean.getBargainPrice()*listBean.getShopsnum();
                }
            }
        }
        DecimalFormat decimalFormat = new DecimalFormat();
        String format = decimalFormat.format(shopsprice);
        tv_money.setText("总价:¥"+format);

    }

    //解绑
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (shopPresenter!=null){
            shopPresenter.detachView();
        }
    }
}
//////////////////////////////////////////////////////////////////p层
package zjj.bwie.com.shopcar.presenter;

import android.util.Log;

import java.util.HashMap;

import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.model.IShopModel;
import zjj.bwie.com.shopcar.view.IShopView;

public class ShopPresenter {
    private IShopView iShopView;
    private IShopModel iShopModel;

    public ShopPresenter(IShopView iShopView) {
        this.iShopView = iShopView;
        this.iShopModel = new IShopModel();
    }
    public void getShops(HashMap<String,String> map, String url){
        iShopModel.getShops(map, url, new IShopModel.ShopsCallBack() {
            @Override
            public void onSuccess(ShopBean shopBean) {
                if(iShopView!=null){

                    iShopView.onsuccess(shopBean);
                }
            }

            @Override
            public void onError(String msg) {
                if(iShopView!=null){

                    iShopView.onfaliure(msg);
                }
            }
        });
    }

    //解绑
    public void detachView(){
        this.iShopView=null;
    }
}
////////////////////////////////////////////////////////////////m层
package zjj.bwie.com.shopcar.model;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Response;
import zjj.bwie.com.shopcar.bean.ShopBean;
import zjj.bwie.com.shopcar.common.OkHttpUtils;

public class IShopModel {

    Handler handler=new Handler();

    public void getShops(HashMap<String,String> map, String url, final ShopsCallBack shopsCallback){
        OkHttpUtils.getinstance().post(url, map, new OkHttpUtils.RequestCallBack() {
            @Override
            public void onSuccess(Response response) {
                try {
                    String s = response.body().string();
                    if(!TextUtils.isEmpty(s)){
                        Log.i("ssssss",s+"");
                        parseResult(s,shopsCallback);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(IOException msg) {
                if(shopsCallback!=null){
                    shopsCallback.onError("网络有异常,请稍后再试");
                }
            }
        });
    }
    private void parseResult(String s, final ShopsCallBack shopsCallback) {
        final ShopBean shopBean = new Gson().fromJson(s, ShopBean.class);
        if(shopBean!=null && shopsCallback!=null){
            handler.post(new Runnable() {
                @Override
                public void run() {
                    shopsCallback.onSuccess(shopBean);
                }
            });
        }
    }

    public interface ShopsCallBack{
        void onSuccess(ShopBean shopBean);
        void onError(String msg);
    }
}

///////////////////////////////////////////////////////////////////v层
package zjj.bwie.com.shopcar.view;

import zjj.bwie.com.shopcar.bean.ShopBean;

public interface IShopView {
    void onsuccess(ShopBean shopBean);
    void onfaliure(String msg);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值