一级购物车的展示(代码篇)

本文详细介绍了一种流行的软件架构模式——MVP(Model-View-Presenter)的具体实现方式,包括M层(业务逻辑层)、P层(Presenter层)、V层(视图层)的设计与交互流程,并展示了具体的代码实现细节。

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

注意,我们利用的是MVP,所以一定要分包分类,规范使用


M层吐舌头

public class MainModel implements IMainModel {


    private Handler handler = new Handler(Looper.getMainLooper());

   public void getGoods(final OnNetListener<GoosBean> onNetListener) {
        HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(goosBean);
                    }
                });
            }
        });
    }
}


M层的接口生气

public interface IMainModel {
    public void getGoods(OnNetListener<GoosBean> onNetListener);
}


P层可怜


public class ShowPresenter {
    public IShowModel iShowModel;
    public IMainActivity iMainActivity;


    public ShowPresenter(IMainActivity iMainActivity) {
        iShowModel = new ShowModel();
        this.iMainActivity = iMainActivity;
    }


    //创建方法
    public void getNews(){
        iShowModel.getGoods(new OnNetListener<GoosBean>() {
            @Override
            public void onSuccess(GoosBean goosBean) {
                ArrayList<GoosBean.DataBean.DatasBean> datas = new ArrayList<GoosBean.DataBean.DatasBean>();
                List<GoosBean.DataBean> data = goosBean.getData();
                for (int i = 0; i <data.size() ; i++) {


                    List<GoosBean.DataBean.DatasBean> datasBeen = data.get(i).getDatas();
                    datas.addAll(datasBeen);
                }
                iMainActivity.show(datas);


            }


            @Override
            public void onFailure(Exception e) {


            }
        });
    }
}



V层快哭了


 public class MainActivity extends AppCompatActivity implements IMainActivity {


    private CheckBox mCheckbox2;
    /**
     * 0
     */
    private TextView mTvPrice;
    /**
     * 结算(0)
     */
    private TextView mTvNum;
    private ShowPresenter showPresenter;
    private RecyclerView mRv;
    private MyAdapter myAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        showPresenter = new ShowPresenter(this);
        showPresenter.getNews();
        mRv.setLayoutManager(new LinearLayoutManager(this));


        //注册一下eventbus
        EventBus.getDefault().register(this);
        //点击全选按钮,改变其他的状态值
        mCheckbox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAdapter.allSelect(mCheckbox2.isChecked());
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void initView() {
        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
        mTvPrice = (TextView) findViewById(R.id.tv_price);
        mTvNum = (TextView) findViewById(R.id.tv_num);
        mRv = (RecyclerView) findViewById(R.id.rv);
    }


    @Override
    public void show(List<GoosBean.DataBean.DatasBean> list) {
        myAdapter = new MyAdapter(list, this);
        mRv.setAdapter(myAdapter);
    }
    @Subscribe
    public void MessageEvent(MessageEvent event){
        mCheckbox2.setChecked(event.isChecked());
    }


    @Subscribe惊讶
    public void MessageEvent(PriceAndCountEvent event){


        mTvNum.setText("结算:"+event.getCount());
        mTvPrice.setText(event.getPrice()+"");
    }
}
   
V层接口惊讶


public interface IMainActivity {
    public void show(List<GoosBean.DataBean.DatasBean> list);
}

   

别忘了net层的东东

API的url生气

public interface Api {
    public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
}
 

 HttpUtils哭

public class HttpUtils {
    private static volatile HttpUtils httpUtils;
    private final OkHttpClient client;


    private HttpUtils() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();
    }


    public static HttpUtils getHttpUtils() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }


    /**
     * GET请求
     *
     * @param url
     * @param callback
     */
    public void doGet(String url, Callback callback) {
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }
}


接口微笑

public interface OnNetListener<T> {
    public void onSuccess(T t);


    public void onFailure(Exception e);
}


eventbus文件夹下的东东

public class MessageEvent {
    private boolean checked;


    public boolean isChecked() {
        return checked;
    }


    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}


public class PriceAndCountEvent {
    private int price;
    private int count;


    public int getPrice() {
        return price;
    }


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


    public int getCount() {
        return count;
    }


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



最最最重要的就是adapter层的东东了
疑问鄙视害羞


public class MyAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<GoosBean.DataBean.DatasBean> list;
    private Context context;


    //创建自己的构造方法
    public MyAdapter(List<GoosBean.DataBean.DatasBean> list, Context context) {
        this.list = list;
        this.context = context;


    }
    //加载布局
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.child,parent,false);
        return new MyViewHolder(view);
    }
   //绑定数据
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final GoosBean.DataBean.DatasBean datasBean = list.get(position);
        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.cbChild.setChecked(datasBean.isChecked());
        myViewHolder.tv_tel.setText(datasBean.getType_name());
        myViewHolder.tv_content.setText(datasBean.getMsg());
        myViewHolder.tv_time.setText(datasBean.getAdd_time());
        myViewHolder.tv_price.setText(datasBean.getPrice() + "");
        myViewHolder.myView.setNum(datasBean.getNum() + "");


        //给checkbox设置点击事件
        myViewHolder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //首先改变当前checkbox的状态值
                datasBean.setChecked(myViewHolder.cbChild.isChecked());
                PriceAndCountEvent compute = compute();
                EventBus.getDefault().post(compute);
                //如果改变
                if(myViewHolder.cbChild.isChecked()){
                    //查看是否全选
                    if(isAllCbSelect()){
                        //改变全选按钮的状态
                         changeAllCbState(true);


                    }
                }else{
                    changeAllCbState(false);
                }
                notifyDataSetChanged();
            }
        });
        //点击增加按钮
        myViewHolder.myView.setAddClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取数量和价钱
                int num = myViewHolder.myView.getNum();
                num++;
                datasBean.setNum(num);
                //如果勾选了,
                if(myViewHolder.cbChild.isChecked()){
                    EventBus.getDefault().post(compute());
                }
                notifyDataSetChanged();
            }
        });


        //减少按钮的点击
        myViewHolder.myView.setDelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取数量和价钱
                int num = myViewHolder.myView.getNum();
                if(num==1){
                    return;
                }
                num--;
                datasBean.setNum(num);
                //如果勾选了,
                if(myViewHolder.cbChild.isChecked()){
                    EventBus.getDefault().post(compute());
                }
                notifyDataSetChanged();
            }
        });


        //删除按钮的
        myViewHolder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                //发送价钱和数量
                EventBus.getDefault().post(compute());
                //刷新
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }
//创建自定义viewholder,继承viewholder
    class MyViewHolder extends RecyclerView.ViewHolder{
        private final CheckBox cbChild;
        private final TextView tv_tel;
        private final TextView tv_content;
        private final TextView tv_time;
        private final TextView tv_price;
        private MyView myView;
        private final TextView tv_del;
        public MyViewHolder(View itemView) {
            super(itemView);
            cbChild = (CheckBox) itemView.findViewById(R.id.cb_child);
            tv_tel = (TextView) itemView.findViewById(R.id.tv_tel);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_price = (TextView) itemView.findViewById(R.id.tv_pri);
            tv_del = (TextView) itemView.findViewById(R.id.tv_del);
            myView = (MyView) itemView.findViewById(R.id.mv);
        }
    }


    //查看checkbox是否全选
    public boolean isAllCbSelect(){
        for (int i = 0; i <list.size() ; i++) {
            GoosBean.DataBean.DatasBean datasBean = list.get(i);
            if(!datasBean.isChecked()){
                return false;
            }
        }
        return true;
    }




    //改变全选按钮的状态
    public void changeAllCbState(boolean flag){
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }


    
    //创建一个方法传递价钱和数量
    public PriceAndCountEvent compute(){
        int price = 0;
        int count = 0;
        for (int i = 0; i <list.size() ; i++) {
            GoosBean.DataBean.DatasBean datasBean = list.get(i);
            //如果勾选了
            if(datasBean.isChecked()){
                //进行价钱和数量的++
                count += datasBean.getNum();
                price += datasBean.getNum()*datasBean.getPrice();
            }
        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }


    //设置全选,反选的方法(点击全选,改变列表的状态值)
    public void allSelect(boolean flag){


        for (int i = 0; i <list.size() ; i++) {
            GoosBean.DataBean.DatasBean datasBean = list.get(i);
            datasBean.setChecked(flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }

}


还有一个并不是特别重要的bean类


public class GoosBean {


    /**
     * code : 200
     * data : [{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}],"title":"苹果","title_id":"59280"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"小米4s (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"小米5s (亮黑色)","type_sn_id":"tgg"}],"title":"小米","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"三星 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"三星 (亮黑色)","type_sn_id":"tgg"}],"title":"三星","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"华为 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"华为 (亮黑色)","type_sn_id":"tgg"},{"add_time":"2016-12-10 4:55:28","cart_id":"445164","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":150,"type_name":"华为 (纯黑色)","type_sn_id":"hgg"}],"title":"华为","title_id":"59279"}]
     * flag : Success
     * msg : 描述
     */


    private String code;
    private String flag;
    private String msg;
    private List<DataBean> data;


    public String getCode() {
        return code;
    }


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


    public String getFlag() {
        return flag;
    }


    public void setFlag(String flag) {
        this.flag = flag;
    }


    public String getMsg() {
        return msg;
    }


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


    public List<DataBean> getData() {
        return data;
    }


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


    public static class DataBean {
        /**
         * datas : [{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}]
         * title : 苹果
         * title_id : 59280
         */
        private boolean checked;
        private String title;
        private String title_id;
        private List<DatasBean> datas;


        public boolean isChecked() {
            return checked;
        }


        public void setChecked(boolean checked) {
            this.checked = checked;
        }


        public String getTitle() {
            return title;
        }


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


        public String getTitle_id() {
            return title_id;
        }


        public void setTitle_id(String title_id) {
            this.title_id = title_id;
        }


        public List<DatasBean> getDatas() {
            return datas;
        }


        public void setDatas(List<DatasBean> datas) {
            this.datas = datas;
        }


        public static class DatasBean {
            /**
             * add_time : 2016-12-10 14:54:58
             * cart_id : 445162
             * house_id : 1
             * msg : 购买渠道:大陆国行
             * price : 500
             * type_name : 苹果 iPhone 6(白金色)
             * type_sn_id : ggh
             */
            private boolean checked;
            private int num = 1;
            private String add_time;
            private String cart_id;
            private String house_id;
            private String msg;
            private int price;
            private String type_name;
            private String type_sn_id;


            public int getNum() {
                return num;
            }


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


            public boolean isChecked() {
                return checked;
            }


            public void setChecked(boolean checked) {
                this.checked = checked;
            }


            public String getAdd_time() {
                return add_time;
            }


            public void setAdd_time(String add_time) {
                this.add_time = add_time;
            }


            public String getCart_id() {
                return cart_id;
            }


            public void setCart_id(String cart_id) {
                this.cart_id = cart_id;
            }


            public String getHouse_id() {
                return house_id;
            }


            public void setHouse_id(String house_id) {
                this.house_id = house_id;
            }


            public String getMsg() {
                return msg;
            }


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


            public int getPrice() {
                return price;
            }


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


            public String getType_name() {
                return type_name;
            }


            public void setType_name(String type_name) {
                this.type_name = type_name;
            }


            public String getType_sn_id() {
                return type_sn_id;
            }


            public void setType_sn_id(String type_sn_id) {
                this.type_sn_id = type_sn_id;
            }
        }
    }
}
    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值