美文网首页
shoppingcart

shoppingcart

作者: 串串大侠 | 来源:发表于2018-11-21 15:49 被阅读0次
    maimactivity页面

    main_activity.xml布局


    <?xml version="1.0" encoding="utf-8"?>

        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"

        android:orientation="vertical"

        tools:context="com.umeng.soexample.shopcat01.MainActivity">

        android:id="@+id/elc_show_main"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1"

        android:background="#fff"

        >

            android:layout_width="match_parent"

            android:layout_height="60dp"

            android:gravity="center_vertical"

            android:orientation="horizontal"

            >

                android:id="@+id/cb_allCheck_main"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" />

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="全选"

                android:textColor="#000"

                />

                android:id="@+id/btn_allprice_main"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_marginLeft="10dp"

                android:layout_weight="1"

                android:text="价格"

                android:textColor="@color/colorAccent"

                android:textSize="20sp"

                />

                android:id="@+id/btn_allNumber_main"

                android:layout_width="120dp"

                android:layout_height="wrap_content"

                android:text="去结算(0)"

                />

    mainactivity页面

    package com.umeng.soexample.shopcat01;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.CheckBox;

    import android.widget.ExpandableListView;

    import android.widget.TextView;

    import com.google.gson.Gson;

    import com.umeng.soexample.shopcat01.adapter.MyAdapter;

    import com.umeng.soexample.shopcat01.bean.CartInfo;

    import java.io.IOException;

    import java.util.List;

    import okhttp3.Call;

    import okhttp3.Callback;

    import okhttp3.OkHttpClient;

    import okhttp3.Request;

    import okhttp3.Response;

    public class MainActivityextends AppCompatActivityimplements View.OnClickListener{

    Stringurl ="http://www.zhaoapi.cn/product/getCarts?uid=71";

    private ExpandableListViewelc_show_main;

    private CheckBoxcb_allCheck_main;

    private TextViewbtn_allPrice_main;

    private Buttonbtn_allNumber_main;

    private MyAdaptermyAdapter;

    @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    initView();

    initData();

    }

    private void initData() {

    //得到okhttp

            OkHttpClient okHttpClient =new OkHttpClient.Builder().build();

    Request request =new Request.Builder().url(url).build();

    okHttpClient.newCall(request).enqueue(new Callback() {

    @Override

                public void onFailure(Call call, IOException e) {

    }

    @Override

                public void onResponse(final Call call, Response response)throws IOException {

    final String string = response.body().string();

    runOnUiThread(new Runnable() {

    private ListsellerData;

    @Override

                        public void run() {

    Gson gson =new Gson();

    CartInfo cartInfo = gson.fromJson(string, CartInfo.class);

    sellerData = cartInfo.getData();

    //创建适配

    //创建适配

                            myAdapter =new MyAdapter(sellerData,MainActivity.this);

    //调用适配器的借口 来实现回电数据

                            myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {

    @Override

                                public void SellerSelectedChange(int groupPosition) {

    //先得到 checkbox

                                    boolean b =myAdapter.isCurrentSellerAllProductSelected(groupPosition);

    //改变所有当前商家的选中状态

                                    myAdapter.changeCurrentSellerAllProductSelected(groupPosition,!b);

    myAdapter.notifyDataSetChanged();

    refreshAllSelectedAndTotalPriceAndTotalNumber();

    }

    @Override

                                public void changeCurrentProductSelected(int groupPosition,int childPosition) {

    myAdapter.changeCurrentProductSelected(groupPosition,childPosition);

    myAdapter.notifyDataSetChanged();

    refreshAllSelectedAndTotalPriceAndTotalNumber();

    }

    @Override

                                public void ProductNumberChange(int groupPosition,int childPosition,int number) {

    myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);

    myAdapter.notifyDataSetChanged();

    //刷新底部的方法

                                    refreshAllSelectedAndTotalPriceAndTotalNumber();

    }

    });

    elc_show_main.setAdapter(myAdapter);

    //

                            for (int i =0; i

    elc_show_main.expandGroup(i);

    }

    }

    });

    }

    });

    }

    private void initView() {

    elc_show_main = (ExpandableListView) findViewById(R.id.elc_show_main);

    cb_allCheck_main = (CheckBox) findViewById(R.id.cb_allCheck_main);

    btn_allPrice_main = (TextView) findViewById(R.id.btn_allprice_main);

    btn_allNumber_main = (Button) findViewById(R.id.btn_allNumber_main);

    cb_allCheck_main.setOnClickListener(this);

    btn_allNumber_main.setOnClickListener(this);

    }

    private void  refreshAllSelectedAndTotalPriceAndTotalNumber(){

    boolean allProductsSelected =myAdapter.isAllProductsSelected();

    cb_allCheck_main.setChecked(allProductsSelected);

    //计算总金额

            Double totalPrice =myAdapter.calculateTotalPrice();

    btn_allPrice_main.setText("总价:¥"+totalPrice);

    //计算总数量

            int totalNumber =myAdapter.calculateTotalNumber();

    btn_allNumber_main.setText("去结算("+totalNumber+")");

    }

    @Override

        public void onClick(View v) {

    switch (v.getId()) {

    case R.id.btn_allNumber_main:

    break;

    case R.id.cb_allCheck_main:

    boolean allProductsSelected =myAdapter.isAllProductsSelected();

    myAdapter.changeAllProductsSelected(!allProductsSelected);

    myAdapter.notifyDataSetChanged();

    //刷新底部的方法

                    refreshAllSelectedAndTotalPriceAndTotalNumber();

    break;

    }

    }

    }

    MyAdapter页面

    package com.umeng.soexample.shopcat01.adapter;

    import android.content.Context;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseExpandableListAdapter;

    import android.widget.CheckBox;

    import android.widget.ImageView;

    import android.widget.TextView;

    import com.bumptech.glide.Glide;

    import com.squareup.picasso.Picasso;

    import com.umeng.soexample.shopcat01.MyAddSubView;

    import com.umeng.soexample.shopcat01.R;

    import com.umeng.soexample.shopcat01.bean.CartInfo;

    import java.util.List;

    /**

    * Created by lenovo on 2018-11-20.

    */

    public class MyAdapterextends BaseExpandableListAdapter{

    ListsellerData;

    Contextcontext;

    public MyAdapter(List sellerData, Context context) {

    this.sellerData = sellerData;

    this.context = context;

    }

    @Override

        public int getGroupCount() {

    return sellerData.size();

    }

    @Override

        public int getChildrenCount(int groupPosition) {

    return sellerData.get(groupPosition).getList().size();

    }

    @Override

        public Object getGroup(int groupPosition) {

    return null;

    }

    @Override

        public Object getChild(int groupPosition,int childPosition) {

    return null;

    }

    @Override

        public long getGroupId(int groupPosition) {

    return 0;

    }

    @Override

        public long getChildId(int groupPosition,int childPosition) {

    return 0;

    }

    @Override

        public boolean hasStableIds() {

    return false;

    }

    //设置外层

        @Override

        public View getGroupView(final int groupPosition,boolean isExpanded, View convertView, ViewGroup parent) {

    GroupViewHolder groupViewHolder =null;

    if (convertView ==null){

    groupViewHolder =new GroupViewHolder();

    convertView = View.inflate(context, R.layout.group_view_layout_shopcar,null);

    groupViewHolder.cb_group_item = convertView.findViewById(R.id.cb_group_item);

    groupViewHolder.tv_title_group = convertView.findViewById(R.id.tv_title_group);

    convertView.setTag(groupViewHolder);

    }else {

    groupViewHolder = (GroupViewHolder) convertView.getTag();

    }

    //赋值

            CartInfo.DataBean dataBean =sellerData.get(groupPosition);

    //商家名称

            groupViewHolder.tv_title_group.setText(dataBean.getSellerName());

    boolean b = isCurrentSellerAllProductSelected(groupPosition);

    //商家的checkbox

            groupViewHolder.cb_group_item.setChecked(b);

    //点击checkbox触发监听

            groupViewHolder.cb_group_item.setOnClickListener(new View.OnClickListener() {

    @Override

                public void onClick(View v) {

    if (onCartListChangeListener !=null) {

    onCartListChangeListener.SellerSelectedChange(groupPosition);

    }

    }

    });

    return convertView;

    }

    public void changeCurrentSellerAllProductSelected(int Position,boolean b) {

    List list =sellerData.get(Position).getList();

    for (int i =0; i < list.size(); i++) {

    list.get(i).setSelected(b ?1 :0);

    }

    }

    //用来判断外层的Selected值 如果是0 不选中 否则选中

        public boolean isCurrentSellerAllProductSelected(int position) {

    List list =sellerData.get(position).getList();

    for (int i =0; i < list.size(); i++) {

    if (list.get(i).getSelected() ==0) {

    return false;

    }

    }

    return true;

    }

    //设置子层

        @Override

        public View getChildView(final int groupPosition,final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {

    ChildViewHolder childViewHolder =null;

    if (convertView ==null) {

    childViewHolder =new ChildViewHolder();

    convertView = View.inflate(context, R.layout.child_view_layout_shopcar,null);

    childViewHolder.cb_child_item = convertView.findViewById(R.id.cb_child_item);

    childViewHolder.tv_title_child = convertView.findViewById(R.id.tv_title_child);

    childViewHolder.tv_price_child = convertView.findViewById(R.id.tv_price_child);

    childViewHolder.iv_icon_child= convertView.findViewById(R.id.iv_icon_child);

    childViewHolder.add_sub_view_child = convertView.findViewById(R.id.add_sub_view_child);

    convertView.setTag(childViewHolder);

    }else {

    childViewHolder = (ChildViewHolder) convertView.getTag();

    }

    //赋值

            CartInfo.DataBean.ListBean listBean =sellerData.get(groupPosition).getList().get(childPosition);

    //商品名称

            childViewHolder.tv_title_child.setText(listBean.getTitle());

    //商品图片

            String images = listBean.getImages();

    String[] split = images.split("\\|");

    Glide.with(context).load(split[0]).into(childViewHolder.iv_icon_child);

    //商品价格

            childViewHolder.tv_price_child.setText("¥" + listBean.getPrice());

    //商品checkb0x

            childViewHolder.cb_child_item.setChecked(listBean.getSelected() ==1);

    childViewHolder.cb_child_item.setOnClickListener(new View.OnClickListener() {

    @Override

                public void onClick(View v) {

    onCartListChangeListener.changeCurrentProductSelected(groupPosition,childPosition);

    }

    });

    childViewHolder.add_sub_view_child.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {

    @Override

                public void OnNumberChange(int number) {

    onCartListChangeListener.ProductNumberChange(groupPosition,childPosition, number);

    }

    });

    return convertView;

    }

    public void changeCurrentProductNumber(int groupPosition,int childPosition,int number) {

    CartInfo.DataBean.ListBean listBean =sellerData.get(groupPosition).getList().get(childPosition);

    listBean.setNum(number);

    }

    public void changeCurrentProductSelected(int groupPosition,int childPosition) {

    CartInfo.DataBean.ListBean listBean =sellerData.get(groupPosition).getList().get(childPosition);

    listBean.setSelected(listBean.getSelected() ==0 ?1 :0);

    }

    public double calculateTotalPrice() {

    double totalPrice =0;

    for (int i =0; i

    List list =sellerData.get(i).getList();

    for (int j =0; j < list.size(); j++) {

    //只有选中采取计算

                    if (list.get(j).getSelected() ==1) {

    double price = list.get(j).getPrice();

    int num = list.get(j).getNum();

    totalPrice += price * num;

    }

    }

    }

    return totalPrice;

    }

    public int calculateTotalNumber() {

    int totalNumber =0;

    for (int i =0; i

    List list =sellerData.get(i).getList();

    for (int j =0; j < list.size(); j++) {

    //只有选中采取计算

                    if (list.get(j).getSelected() ==1) {

    int num = list.get(j).getNum();

    totalNumber += num;

    }

    }

    }

    return totalNumber;

    }

    @Override

        public boolean isChildSelectable(int groupPosition,int childPosition) {

    return false;

    }

    public boolean isAllProductsSelected() {

    for (int i =0; i

    List list =sellerData.get(i).getList();

    for (int j =0; j < list.size(); j++) {

    if (list.get(j).getSelected() ==0) {

    return false;

    }

    }

    }

    return true;

    }

    public void changeAllProductsSelected(boolean b) {

    for (int i =0; i

    List list =sellerData.get(i).getList();

    for (int j =0; j < list.size(); j++) {

    list.get(j).setSelected(b ?1 :0);

    }

    }

    }

    public static class GroupViewHolder {

    public CheckBoxcb_group_item;

    public TextViewtv_title_group;

    }

    public static class ChildViewHolder {

    public CheckBoxcb_child_item;

    public ImageViewiv_icon_child;

    public TextViewtv_title_child;

    public TextViewtv_price_child;

    public MyAddSubViewadd_sub_view_child;

    }

    OnCartListChangeListeneronCartListChangeListener;

    public void setOnCartListChangeListener(OnCartListChangeListener onCartListChangeListener) {

    this.onCartListChangeListener = onCartListChangeListener;

    }

    public interface OnCartListChangeListener {

    void SellerSelectedChange(int groupPosition);

    void changeCurrentProductSelected(int groupPosition,int childPosition);

    void ProductNumberChange(int groupPosition,int childPosition,int number);

    }

    }

    cartinfo是bean类 实现 接口

    add_sub_view.xml布局


    <?xml version="1.0" encoding="utf-8"?>

        android:layout_width="80dp"

        android:layout_height="30dp"

        android:layout_gravity="center_vertical"

        android:background="#000"

        android:orientation="horizontal"

        android:padding="2dp"

        >

            android:id="@+id/tv_sub_view"

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_weight="1"

            android:background="#fff"

            android:gravity="center"

            android:text="-"

            android:textColor="#000"

            />

            android:id="@+id/tv_number_view"

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_marginLeft="2dp"

            android:layout_marginRight="2dp"

            android:layout_weight="1"

            android:background="#fff"

            android:gravity="center"

            android:text="1"

            android:textColor="#000"

            />

            android:id="@+id/tv_add_view"

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_weight="1"

            android:background="#fff"

            android:gravity="center"

            android:text="+"

            android:textColor="#000"

            />

    MyAddSubView自定义控件页面

    package com.umeng.soexample.shopcat01;

    import android.content.Context;

    import android.support.annotation.Nullable;

    import android.util.AttributeSet;

    import android.view.View;

    import android.widget.LinearLayout;

    import android.widget.TextView;

    import android.widget.Toast;

    /**

    * Created by lenovo on 2018-11-20.

    */

    public class MyAddSubViewextends LinearLayoutimplements View.OnClickListener{

    TextViewtv_sub_view;

    TextViewtv_number_view;

    TextViewtv_add_view;

    private int number =1;

    public int getNumber() {

    return number;

    }

    public void setNumber(int number) {

    this.number = number;

    tv_number_view.setText(number+"");

    }

    public MyAddSubView(Context context) {

    super(context,null);

    }

    public MyAddSubView(Context context,@Nullable AttributeSet attrs) {

    super(context, attrs);

    View inflate = View.inflate(context, R.layout.add_sub_view,this);

    tv_sub_view = inflate.findViewById(R.id.tv_sub_view);

    tv_number_view = inflate.findViewById(R.id.tv_number_view);

    tv_add_view = inflate.findViewById(R.id.tv_add_view);

    tv_add_view.setOnClickListener(this);

    tv_sub_view.setOnClickListener(this);

    }

    OnNumberChangeListeneronNumberChangeListener;

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {

    this.onNumberChangeListener = onNumberChangeListener;

    }

    @Override

        public void onClick(View v) {

    switch (v.getId()){

    case R.id.tv_sub_view:

    if (number>1){

    --number;

    tv_number_view.setText(number+"");

    if (onNumberChangeListener !=null){

    onNumberChangeListener.OnNumberChange(number);

    }

    }else {

    Toast.makeText(getContext(),"不能再少啦!", Toast.LENGTH_SHORT).show();

    }

    break;

    case R.id.tv_add_view:

    ++number;

    tv_number_view.setText(number+"");

    if (onNumberChangeListener!=null){

    onNumberChangeListener.OnNumberChange(number);

    }

    break;

    }

    }

    public interface OnNumberChangeListener {

    void OnNumberChange(int number);

    }

    }

    ground_view_layout_shopcar.xml布局


    <?xml version="1.0" encoding="utf-8"?>

        android:layout_width="match_parent"

        android:layout_height="60dp"

        android:gravity="center_vertical"

        android:orientation="horizontal"

        >

            android:id="@+id/cb_group_item"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

            android:id="@+id/tv_title_group"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            android:ellipsize="end"

            android:maxLines="2"

            android:text="标题"

            android:textColor="#000"

            android:textSize="20sp"

            />

    child_view_layout_shopcar.xml


    <?xml version="1.0" encoding="utf-8"?>

        android:layout_width="match_parent"

        android:layout_height="120dp"

        android:gravity="center_vertical"

        android:orientation="horizontal"

        >

            android:id="@+id/cb_child_item"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            />

            android:id="@+id/iv_icon_child"

            android:layout_width="80dp"

            android:layout_height="80dp"

            android:scaleType="centerCrop"

            android:src="@mipmap/ic_launcher"

            />

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_marginLeft="10dp"

            android:layout_weight="1"

            android:gravity="center_vertical"

            android:orientation="vertical"

            >

                android:id="@+id/tv_title_child"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:ellipsize="end"

                android:maxLines="2"

                android:text="标题"

                android:textColor="#000"

                />

                android:id="@+id/tv_price_child"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="标题"

                android:textColor="@color/colorPrimary"

                android:textSize="20sp"

                />

        android:id="@+id/add_sub_view_child"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">

    相关文章

      网友评论

          本文标题:shoppingcart

          本文链接:https://www.haomeiwen.com/subject/awjcqqtx.html