MVP模式购物车

这篇博客探讨了如何运用MVP(Model-View-Presenter)设计模式来实现一个购物车系统,主要关注购物车订单列表的创建和管理。通过Api类与后台交互,完成商品的添加、删除及更新操作。

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



public class PriceAndCount {
    private double price;
    private int count;

    public PriceAndCount(double price, int count) {
        this.price = price;
        this.count = count;
    }

    public double getPrice() {
        return price;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
不能忘了这两行,勾选用
  private boolean checked;
            private int count=1;



net包

Api类

public interface Api {
    boolean ISONLINE = true;
    String DEV = "http://169.27.23.105";
    public static String ONLINE = "http://120.27.23.105";

    public static String HOST = ISONLINE ? ONLINE : DEV;
    public static String Login = HOST + "/user/login";
    public static String REGISTER = HOST + "/user/reg";
    public static String CATAGORY = HOST + "/product/getCatagory";
    public static String PRODUCT_CATAGORY = HOST + "/product/getProductCatagory?cid=%s";
    public static String PRODUCT_CATAGORY_LIST = HOST + "/product/getProducts";
    public static String PRODUCT_DETAIL = HOST + "/product/getProductDetail?pid=%s&source=android";
    public static String ADD_CARD = HOST + "/product/addCart";
    public static String SELECT_CARD = HOST + "/product/getCarts";
    public static String DEL_CARD = HOST + "/product/deleteCart";


    public static String str1 = "https://www.zhaoapi.cn/product/getProductDetail";
    public static String str2 = "https://www.zhaoapi.cn/product/getCarts";
}

Httputils类

public class HttpUtils {
    private static HttpUtils httpUtils;
    private final OkHttpClient client;
    private HttpUtils(){
        HttpLoggingInterceptor logging=new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        client=new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
    }
    public static HttpUtils getHttpUtils(){
        if(httpUtils==null){
            synchronized (HttpUtils.class){
                if(httpUtils==null){
                    httpUtils=new HttpUtils();
                }
            }
        }
        return httpUtils;
    }
    /*
    * GET请求*/
    public void doGet(String url, Callback callback){
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * POST请求
     *
     * @param url
     * @param params
     * @param callback
     */
    public void doPost(String url, Map<String, String> params, Callback callback) {
        if (params == null) {
            throw new RuntimeException("参数为空了");
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        Log.e("OkHttpUtils", "请求地址:" + url + " 请求的参数:");
        FormBody formBody = builder.build();
        Request request = new Request.Builder().url(url).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }
}

拦截器

public class MyInterceptor implements Interceptor{
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.method().equals("GET")) {
            String url = request.url().url().toString();
            url += "&source=android";
            Request newRequset = request.newBuilder().url(url).build();
            return chain.proceed(newRequset);
        } else {
            FormBody formBody = (FormBody) request.body();
            FormBody.Builder builder = new FormBody.Builder();
            for (int i = 0; i < formBody.size(); i++) {
                builder.add(formBody.name(i), formBody.value(i));
            }
            builder.add("source", "android");
            FormBody newFormBody = builder.build();
            Request newRequest = request.newBuilder().url(request.url().url().toString()).post(newFormBody).build();
            return chain.proceed(newRequest);
        }
    }
}
public interface OnNetListener<T> {
    public void onSuccess(T t);
    public void onFaliure(Exception e);
}

ElvAdapter

public class ElvAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GetCart.DataBean> group;
    private List<List<GetCart.DataBean.ListBean>> child;
    private final LayoutInflater inflater;

    public ElvAdapter(Context context, List<GetCart.DataBean> group, List<List<GetCart.DataBean.ListBean>> child) {
        this.context = context;
        this.group = group;
        this.child = child;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view;
        final GroupViewHolder holder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.elv_group, null);
            holder = new GroupViewHolder();
            holder.tv = view.findViewById(R.id.tvGroup);
            holder.cbGroup = view.findViewById(R.id.cbGroup);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (GroupViewHolder) view.getTag();
        }
        final GetCart.DataBean dataBean = group.get(groupPosition);
        holder.tv.setText(dataBean.getSellerName());
        holder.cbGroup.setChecked(dataBean.isChecked());

        holder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //需要改变三个checkbox的状态值
                //1.一级列表的checkbox状态值
                dataBean.setChecked(holder.cbGroup.isChecked());
                //2.二级列表的checkbox状态值
                setChildrenCb(groupPosition, holder.cbGroup.isChecked());
                //3.全选的checkbox状态值
                ((SecondActivity) context).setAllChecked(isAllGroupCbChecked());
                //计算钱和数量并显示
                setPriceAndCount();
                //刷新界面
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, final View convertView, ViewGroup parent) {
        View view;
        final ChildViewHolder holder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.elv_child, null);
            holder = new ChildViewHolder();
            holder.iv = view.findViewById(R.id.iv);
            holder.tvTitle = view.findViewById(R.id.tvTitle);
            holder.tvSubhead = view.findViewById(R.id.tvSubhead);
            holder.tvPrice = view.findViewById(R.id.tvPrice);
            holder.cbChild = view.findViewById(R.id.cbChild);
            holder.btDel = view.findViewById(R.id.btDel);
            holder.tvNum = view.findViewById(R.id.tvNum);
            holder.ivDel = view.findViewById(R.id.ivDel);
            holder.ivAdd = view.findViewById(R.id.ivAdd);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ChildViewHolder) view.getTag();
        }

        final GetCart.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition);
        String images = listBean.getImages();
        Glide.with(context).load(images.split("\\|")[0]).into(holder.iv);
        holder.tvTitle.setText(listBean.getTitle());
        holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked());
        holder.tvSubhead.setText(listBean.getSubhead());
        holder.tvPrice.setText(listBean.getPrice() + "元");
        holder.tvNum.setText(listBean.getCount() + "");
        //给checkbox设置点击事件
        holder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //需要改变三个checkbox的状态值
                //1.二级列表的checkbox状态值
                listBean.setChecked(holder.cbChild.isChecked());
                //2.一级列表的checkbox状态值
                group.get(groupPosition).setChecked(isAllChildCbChecked(groupPosition));
                //3.全选的checkbox状态值
                ((SecondActivity) context).setAllChecked(isAllGroupCbChecked());
                //计算钱和数量并显示
                setPriceAndCount();
                //刷新界面
                notifyDataSetChanged();
            }
        });

        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取目前显示的值
                int count = listBean.getCount();
                count++;
                //改变JavaBean里的状态值
                listBean.setCount(count);
                //计算钱和数量并显示
                setPriceAndCount();
                //刷新列表
                notifyDataSetChanged();
            }
        });
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取目前显示的值
                int count = listBean.getCount();
                if (count <= 1) {
                    count = 1;
                } else {
                    count--;
                }
                //改变JavaBean里的状态值
                listBean.setCount(count);
                //计算钱和数量并显示
                setPriceAndCount();
                //刷新列表
                notifyDataSetChanged();
            }
        });

        holder.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //其实就是删除集合
                List<GetCart.DataBean.ListBean> listBeans = child.get(groupPosition);
                if (listBeans.size() > 0) {
                    listBeans.remove(childPosition);
                }
                if (listBeans.size() == 0) {
                    child.remove(groupPosition);
                    group.remove(groupPosition);
                }
                //计算钱和数量并显示
                setPriceAndCount();
                //改变全选状态
                ((SecondActivity) context).setAllChecked(isAllGroupCbChecked());
                //刷新列表
                notifyDataSetChanged();
            }
        });
        return view;
    }

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

    class GroupViewHolder {
        TextView tv;
        CheckBox cbGroup;
    }

    class ChildViewHolder {
        ImageView iv;
        TextView tvTitle;
        TextView tvSubhead;
        TextView tvPrice;
        CheckBox cbChild;
        Button btDel;
        TextView tvNum;
        ImageView ivDel;
        ImageView ivAdd;
    }

    /**
     * 设置一级列表对应的二级列表checkbox状态
     *
     * @param groupPosition
     * @param bool
     */
    private void setChildrenCb(int groupPosition, boolean bool) {
        List<GetCart.DataBean.ListBean> listBeans = child.get(groupPosition);
        for (int i = 0; i < listBeans.size(); i++) {
            listBeans.get(i).setChecked(bool);
        }
    }

    /**
     * 判断一级列表checkbox状态
     *
     * @return
     */
    private boolean isAllGroupCbChecked() {
        if (group.size() == 0) {
            return false;
        }
        for (int i = 0; i < group.size(); i++) {
            if (!group.get(i).isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断二级列表checkbox状态
     *
     * @return
     */
    private boolean isAllChildCbChecked(int groupPosition) {
        List<GetCart.DataBean.ListBean> listBeans = child.get(groupPosition);
        for (int i = 0; i < listBeans.size(); i++) {
            if (!listBeans.get(i).isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 设置钱和数量
     */
    private void setPriceAndCount() {
        ((SecondActivity) context).setPriceAndCount(compute());
    }

    /**
     * 计算钱和数量
     */
    private PriceAndCount compute() {
        double price = 0;
        int count = 0;
        for (int i = 0; i < group.size(); i++) {
            List<GetCart.DataBean.ListBean> listBeans = child.get(i);
            for (int j = 0; j < listBeans.size(); j++) {
                if (listBeans.get(j).isChecked()) {
                    price += listBeans.get(j).getPrice() * listBeans.get(j).getCount();
                    count += listBeans.get(j).getCount();
                }
            }
        }
        return new PriceAndCount(price, count);
    }

    /**
     * 全选或者全不选
     *
     * @param bool
     */
    public void AllOrNone(boolean bool) {
        for (int i = 0; i < group.size(); i++) {
            group.get(i).setChecked(bool);
            setChildrenCb(i, bool);
        }
        setPriceAndCount();
        notifyDataSetChanged();
    }
}

RvAllAdapter
public class RvAllAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<OrderBean.DataBean> list;

    public RvAllAdapter(Context context, List<OrderBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.order_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        OrderBean.DataBean dataBean = list.get(position);
        myViewHolder.tvTitle.setText(dataBean.getTitle());
        int status = dataBean.getStatus();
        myViewHolder.tvBt.setText("查看订单");
        myViewHolder.tvStatus.setTextColor(Color.parseColor("#000000"));
        if (status == 0) {
            myViewHolder.tvStatus.setText("待支付");
            myViewHolder.tvBt.setText("取消订单");
            myViewHolder.tvStatus.setTextColor(Color.parseColor("#ff0000"));
        } else if (status == 1) {
            myViewHolder.tvStatus.setText("已取消");
        } else if (status == 2) {
            myViewHolder.tvStatus.setText("已支付");
        }

        myViewHolder.tvPrice.setText("价格:" + dataBean.getPrice());
        myViewHolder.tvPrice.setTextColor(Color.parseColor("#ff0000"));
        myViewHolder.tvTime.setText(dataBean.getCreatetime());


    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTitle;
        private final TextView tvStatus;
        private final TextView tvPrice;
        private final TextView tvTime;
        private final TextView tvBt;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.textView);
            tvStatus = itemView.findViewById(R.id.textView2);
            tvPrice = itemView.findViewById(R.id.textView3);
            tvTime = itemView.findViewById(R.id.textView4);
            tvBt = itemView.findViewById(R.id.textView5);

        }
    }
}
fragment

public class AllFragment extends Fragment {

    private RecyclerView rv;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all, null);
        rv = view.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        String url = "https://www.zhaoapi.cn/product/getOrders?uid=71";
        HttpUtils.getHttpUtils().doGet(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 OrderBean orderBean = new Gson().fromJson(string, OrderBean.class);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RvAllAdapter adapter = new RvAllAdapter(getContext(), orderBean.getData());
public class ShangpinModel implements IShangpinModel {
    private Handler handler=new Handler(Looper.getMainLooper());
    //查看商品
    @Override
    public void doPost(Map<String, String> params, final OnNetListener<ShangpinBean> onNetListener) {
        HttpUtils.getHttpUtils().doPost(Api.str1, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

    //加入购物车
    @Override
    public void doAdd(Map<String, String> params, final OnNetListener<AddCartBean> onNetListener) {
        ; HttpUtils.getHttpUtils().doPost(Api.str1, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

    //查询购物车
    @Override
    public void GetCart(Map<String, String> params, final OnNetListener<GetCart> onNetListener) {
        HttpUtils.getHttpUtils().doPost(Api.str2, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

rv.setAdapter(adapter); } }); } }); return view; }}


public class WaitFragment extends Fragment {
    @Nullable

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}
model层
public interface IShangpinModel {
    public void doPost(Map<String,String> params, OnNetListener<ShangpinBean> onNetListener);
    public void doAdd(Map<String,String> params, OnNetListener<AddCartBean> onNetListener);
    public void GetCart(Map<String,String> params, OnNetListener<GetCart> onNetListener);
}



public class ShangpinModel implements IShangpinModel {
    private Handler handler=new Handler(Looper.getMainLooper());
    //查看商品
    @Override
    public void doPost(Map<String, String> params, final OnNetListener<ShangpinBean> onNetListener) {
        HttpUtils.getHttpUtils().doPost(Api.str1, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

    //加入购物车
    @Override
    public void doAdd(Map<String, String> params, final OnNetListener<AddCartBean> onNetListener) {
        ; HttpUtils.getHttpUtils().doPost(Api.str1, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

    //查询购物车
    @Override
    public void GetCart(Map<String, String> params, final OnNetListener<GetCart> onNetListener) {
        HttpUtils.getHttpUtils().doPost(Api.str2, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final GetCart getCart = new Gson().fromJson(response.body().string(), GetCart.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(getCart);
                    }
                });
            }
        });
    }
}
ptstener层
public class ShangpinPrestener {
    private IShangpinModel iShangpinModel;
    private IShangpinView iShangpinView;
    private ISecondListener iSecondListener;

    public ShangpinPrestener(IShangpinView iShangpinView) {
        this.iShangpinView = iShangpinView;
        iShangpinModel=new ShangpinModel();
    }

    public ShangpinPrestener(ISecondListener iSecondListener) {
        this.iSecondListener = iSecondListener;
        iShangpinModel=new ShangpinModel();
    }

    //查看商品
    public void doPost(){
        Map<String,String> params=new HashMap<>();
        params.put("pid","71");
        iShangpinModel.doPost(params, new OnNetListener<ShangpinBean>() {
            @Override
            public void onSuccess(ShangpinBean shangpinBean) {
                if (iShangpinView != null) {
                    iShangpinView.show(shangpinBean);
                }
            }

            @Override
            public void onFaliure(Exception e) {

            }
        });
    }
    //添加购物车
    public void addCart(){
        Map<String,String> params=new HashMap<>();
        params.put("pid", "71");
        params.put("uid", "70");
        iShangpinModel.doAdd(params, new OnNetListener<AddCartBean>() {
            @Override
            public void onSuccess(AddCartBean addCartBean) {
                if(iShangpinView!=null){
                    iShangpinView.addShwo(addCartBean.getCode().equals("0")?"添加成功":"添加失败了");
                }
            }

            @Override
            public void onFaliure(Exception e) {

            }
        });
    }
    //查看购物车
    public void getCart(){
        Map<String, String> params = new HashMap<>();
        params.put("uid", "1234");
        params.put("pid", "71");
        iShangpinModel.GetCart(params, new OnNetListener<GetCart>() {
            @Override
            public void onSuccess(GetCart getCart) {
                if (iSecondListener != null) {
                    List<GetCart.DataBean> group = getCart.getData();
                    List<List<GetCart.DataBean.ListBean>> child = new ArrayList<>();
                    for (int i = 0; i < group.size(); i++) {
                        child.add(group.get(i).getList());
                    }
                    iSecondListener.show(group, child);
                }
            }

            @Override
            public void onFaliure(Exception e) {

            }
        });
    }
}



view层

public interface ISecondListener {
    void show(List<GetCart.DataBean> group,List<List<GetCart.DataBean.ListBean>> child);
}



public interface IShangpinView {
    void show(ShangpinBean shangpinBean);
    void addShwo(String str);
}

Activity

public class ConfirmActivity extends AppCompatActivity  implements View.OnClickListener {
    private TextView mTvLeft;
    /**
     * 立即下单
     */
    private Button mBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm);
        //接收传过来实付款
        Intent intent = getIntent();
        String money = intent.getStringExtra("money");
        initView();
        mTvLeft.setText("实付款:¥" + money);
    }

    private void initView() {
        mTvLeft = (TextView) findViewById(R.id.tvLeft);
        mBt = (Button) findViewById(R.id.bt);
        mBt.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt:
                //跳转到订单页面
                Intent intent = new Intent(ConfirmActivity.this, OrderActivity.class);
                startActivity(intent);
                break;
        }
    }
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener ,IShangpinView{

    private ImageView mIv;
    private TextView mTvBargainPrice;
    private TextView mTvPrice;
    /**
     * 购物车
     */
    private TextView mTvCart;
    /**
     * 加入购物车
     */
    private TextView mTvAddCart;

    private ShangpinPrestener shangpinPrestener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        shangpinPrestener=new ShangpinPrestener(this);
        shangpinPrestener.doPost();

    }

    private void initView() {
        mIv = (ImageView) findViewById(R.id.iv);
        mIv.setOnClickListener(this);
        mTvBargainPrice = (TextView) findViewById(R.id.tvBargainPrice);
        mTvBargainPrice.setOnClickListener(this);
        mTvPrice = (TextView) findViewById(R.id.tvPrice);
        mTvPrice.setOnClickListener(this);
        mTvCart = (TextView) findViewById(R.id.tvCart);
        mTvCart.setOnClickListener(this);
        mTvAddCart = (TextView) findViewById(R.id.tvAddCart);
        mTvAddCart.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tvCart:
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.tvAddCart:
                shangpinPrestener.addCart();
                break;
        }
    }

    @Override
    public void show(ShangpinBean shangpinBean) {
        String images = shangpinBean.getData().getImages();
        String[] split = images.split("\\|");
        Glide.with(this).load(split[0]).into(mIv);

        mTvBargainPrice.setText("原价:"+shangpinBean.getData().getPrice());
        mTvPrice.setText("优惠价:"+shangpinBean.getData().getBargainPrice());
    }

    @Override
    public void addShwo(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
}
public class OrderActivity extends AppCompatActivity {
    /**
     * 全部
     */
    private TextView mTvAll;
    /**
     * 待支付
     */
    private TextView mTvWait;
    private LinearLayout mLl;
    private ViewPager mVp;
    private List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();
        list.add(new AllFragment());
        list.add(new WaitFragment());
        mVp.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }

    private void initView() {
        mTvAll = (TextView) findViewById(R.id.tvAll);
        mTvWait = (TextView) findViewById(R.id.tvWait);
        mLl = (LinearLayout) findViewById(R.id.ll);
        mVp = (ViewPager) findViewById(R.id.vp);
    }
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
public class SecondActivity extends AppCompatActivity implements ISecondListener{

    private ExpandableListView mElv;
    /**
     * 全选
     */
    private CheckBox mCb;
    /**
     * 合计:
     */
    private TextView mTvTotal;
    /**
     * 去结算(0)
     */
    private TextView mTvCount;
    private ShangpinPrestener shangpinPrestener;
    private ElvAdapter elvAdapter;
    private PriceAndCount priceAndCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
        shangpinPrestener=new ShangpinPrestener(this);
        shangpinPrestener.getCart();
        mCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                elvAdapter.AllOrNone(mCb.isChecked());
            }
        });
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCb = (CheckBox) findViewById(R.id.cb);
        mTvTotal = (TextView) findViewById(R.id.tvTotal);
        mTvCount = (TextView) findViewById(R.id.tvCount);
        mTvCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SecondActivity.this,ConfirmActivity.class);
                if(priceAndCount!=null){
                    intent.putExtra("money",priceAndCount.getPrice()+"");

                }
                startActivity(intent);
            }
        });
    }



    @Override
    public void show(List<GetCart.DataBean> group, List<List<GetCart.DataBean.ListBean>> child) {
        elvAdapter = new ElvAdapter(SecondActivity.this,group,child);
        mElv.setGroupIndicator(null);
        mElv.setAdapter(elvAdapter);
        for (int i = 0; i < group.size(); i++) {
            mElv.expandGroup(i);

        }
    }  public void setPriceAndCount(PriceAndCount priceAndCount) {
        this.priceAndCount = priceAndCount;
        mTvTotal.setText("合计:" + priceAndCount.getPrice());
        mTvCount.setText("去结算(" + priceAndCount.getCount() + ")");
    }

    public void setAllChecked(boolean bool) {
        mCb.setChecked(bool);
    }
}
XML
confrim
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_confirm"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="mvpframework .com.gouwuche.ConfirmActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#33000000">

        <TextView
            android:id="@+id/tvLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="#ff0000"
            android:text="立即下单" />
    </RelativeLayout>
</LinearLayout>




main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="mvpframework. .com.gouwuche.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ff3360"
        android:text="商品详情"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="25sp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/iv"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"

        android:id="@+id/tvBargainPrice"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF1100C"
        android:id="@+id/tvPrice"/>
    <View
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#33000000"
            android:id="@+id/tvCart"
            android:text="购物车"
            android:gravity="center"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/tvAddCart"
            android:layout_weight="1"
            android:background="#33000000"
            android:gravity="center"
            android:text="加入购物车"
            />
    </LinearLayout>
</LinearLayout>



order
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_order"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="mvpframework .com.gouwuche.OrderActivity">
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/tvAll"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="全部" />

        <TextView
            android:id="@+id/tvWait"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="待支付" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>



second
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="mvpframework.com.gouwuche.SecondActivity">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/elv"
        android:layout_weight="1"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb"
            android:layout_centerVertical="true"
            android:text="全选"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvTotal"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/cb"
            android:text="合计:"/>
        <TextView
            android:id="@+id/tvCount"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="#ff0000"
            android:gravity="center"
            android:text="去结算(0)"
            android:textColor="#ffffff" />

    </RelativeLayout>
</LinearLayout>




child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="50dp">

    <CheckBox
        android:id="@+id/cbChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

       <!-- <TextView
            android:id="@+id/tvSubhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />-->

        <TextView
            android:id="@+id/tvSubhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_marginLeft="10dp"
                android:id="@+id/ivDel"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/iv_del" />

            <TextView
                android:id="@+id/tvNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:text="1"/>

            <ImageView
                android:id="@+id/ivAdd"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/iv_add" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除" />
</LinearLayout>



group
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cbGroup"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/tvGroup"
        android:gravity="center_vertical"/>
</LinearLayout>



fragment_all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>



order
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:layout_marginTop="21dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="31dp"
        android:layout_marginRight="31dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="27dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView3"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="24dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignEnd="@+id/textView2"
        android:layout_alignRight="@+id/textView2"
        android:text="TextView" />
</RelativeLayout>



效果图






















                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值