二级的购物车


运用网络请求,请求到数据

下面开始展示数据

   activity—main的布局

   

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_cart"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:orientation="vertical"  
  9.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  10.     android:paddingRight="@dimen/activity_horizontal_margin"  
  11.     android:paddingTop="@dimen/activity_vertical_margin"  
  12.     tools:context="bwie.com.xiangmoyisanzoumoli1118.View.CartActivity">  
  13. <ExpandableListView  
  14.      android:id="@+id/elv"  
  15.     android:layout_weight="1"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content" />  
  18.     <LinearLayout  
  19.         android:layout_weight="9"  
  20.         android:orientation="horizontal"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="match_parent">  
  23.         <CheckBox  
  24.             android:id="@+id/quanxuan"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content" />  
  27.         <TextView  
  28.             android:text="全选"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content" />  
  31.          <LinearLayout  
  32.              android:layout_marginLeft="50dp"  
  33.              android:orientation="vertical"  
  34.              android:layout_width="wrap_content"  
  35.              android:layout_height="wrap_content">  
  36.              <TextView  
  37.                  android:id="@+id/zongjia"  
  38.                  android:layout_width="wrap_content"  
  39.                  android:layout_height="wrap_content"  
  40.                  android:text="总价:0.0"  
  41.                  />  
  42.              <TextView  
  43.                  android:text="共0件商品"  
  44.                  android:id="@+id/tv_count"  
  45.                  android:layout_width="wrap_content"  
  46.                  android:layout_height="wrap_content" />  
  47.   
  48.          </LinearLayout>  
  49.            <TextView  
  50.                android:text="去结算"  
  51.                android:background="#FFD82020"  
  52.                 android:layout_marginLeft="100dp"  
  53.                android:layout_width="wrap_content"  
  54.                android:layout_height="wrap_content" />  
  55.     </LinearLayout>  
  56. </LinearLayout>  

MainActivity:  运用到EventBus

[java]  view plain  copy
  1. package bwie.com.xiangmoyisanzoumoli1118.View;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.view.View;  
  6. import android.widget.CheckBox;  
  7. import android.widget.ExpandableListView;  
  8. import android.widget.TextView;  
  9.   
  10. import org.greenrobot.eventbus.EventBus;  
  11. import org.greenrobot.eventbus.Subscribe;  
  12.   
  13. import java.util.ArrayList;  
  14. import java.util.List;  
  15.   
  16. import bwie.com.xiangmoyisanzoumoli1118.Adapter.MyExpandableAdapter;  
  17. import bwie.com.xiangmoyisanzoumoli1118.R;  
  18. import bwie.com.xiangmoyisanzoumoli1118.View.IActivity.IGoodsActivity;  
  19. import bwie.com.xiangmoyisanzoumoli1118.bean.CountAndPrice;  
  20. import bwie.com.xiangmoyisanzoumoli1118.bean.GoodsBean;  
  21. import bwie.com.xiangmoyisanzoumoli1118.bean.MessgeEvent;  
  22. import bwie.com.xiangmoyisanzoumoli1118.persenter.GoodsPersenter;  
  23.   
  24. public class CartActivity extends AppCompatActivity implements IGoodsActivity {  
  25.   
  26.     private ExpandableListView mElv;  
  27.     private List<List<GoodsBean.DataBean.ListBean>> list = new ArrayList<>();  
  28.     private CheckBox mQuanxuan;  
  29.     /** 
  30.      * 总价:0.0 
  31.      */  
  32.     private TextView mZongjia;  
  33.     /** 
  34.      * 共0件商品 
  35.      */  
  36.     private TextView mTvCount;  
  37.      private int count=0;  
  38.      private int price=0;  
  39.     private MyExpandableAdapter myExpandableAdapter;  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_cart);  
  45.         EventBus.getDefault().register(this);  
  46.         //调用GoodsPersenter中的方法  
  47.         new GoodsPersenter(this).getGoods();  
  48.         initView();  
  49.         mQuanxuan.setOnClickListener(new View.OnClickListener() {  
  50.             @Override  
  51.             public void onClick(View v) {  
  52.                 myExpandableAdapter.qx(mQuanxuan.isChecked());  
  53.             }  
  54.         });  
  55.     }  
  56.   
  57.     @Override  
  58.     public void chuanZhi(List<GoodsBean.DataBean> data) {  
  59.         //遍历一级得到二级的集合  
  60.         for (int i = 0; i < data.size(); i++) {  
  61.             List<GoodsBean.DataBean.ListBean> list1 = data.get(i).getList();  
  62.             list.add(list1);  
  63.         }  
  64.         myExpandableAdapter = new MyExpandableAdapter(data, this, list);  
  65.         mElv.setAdapter(myExpandableAdapter);  
  66.         //没有  
  67.         mElv.setGroupIndicator(null);  
  68.         //默认展开  
  69.         for (int i = 0; i < data.size(); i++) {  
  70.             mElv.expandGroup(i);  
  71.         }  
  72.   
  73.   
  74.     }  
  75.   
  76.     private void initView() {  
  77.         mElv = (ExpandableListView) findViewById(R.id.elv);  
  78.   
  79.   
  80.         mQuanxuan = (CheckBox) findViewById(R.id.quanxuan);  
  81.         mZongjia = (TextView) findViewById(R.id.zongjia);  
  82.         mTvCount = (TextView) findViewById(R.id.tv_count);  
  83.     }  
  84.     //接收传过来的值  
  85.     @Subscribe  
  86.     public void onPriceAndCount(CountAndPrice cp){  
  87.   
  88.   
  89.         mZongjia.setText("共"+cp.getCount()+"件商品");  
  90.         mTvCount.setText("总计:"+cp.getPrice());  
  91.   
  92.     }  
  93.     //接收传过来的值  
  94.     @Subscribe  
  95.     public void onPriceAndCount(MessgeEvent event){  
  96.         //改变全选的状态  
  97.         mQuanxuan.setChecked(event.isCheck());  
  98.     }  
  99.     @Override  
  100.     protected void onDestroy() {  
  101.         super.onDestroy();  
  102.         EventBus.getDefault().unregister(this);  
  103.     }  
  104. }  

写二级列表的适配器

[java]  view plain  copy
  1. package bwie.com.xiangmoyisanzoumoli1118.Adapter;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseExpandableListAdapter;  
  7. import android.widget.CheckBox;  
  8. import android.widget.ImageView;  
  9. import android.widget.TextView;  
  10.   
  11. import com.nostra13.universalimageloader.core.ImageLoader;  
  12.   
  13. import org.greenrobot.eventbus.EventBus;  
  14.   
  15. import java.util.List;  
  16.   
  17. import bwie.com.xiangmoyisanzoumoli1118.R;  
  18. import bwie.com.xiangmoyisanzoumoli1118.bean.CountAndPrice;  
  19. import bwie.com.xiangmoyisanzoumoli1118.bean.GoodsBean;  
  20. import bwie.com.xiangmoyisanzoumoli1118.bean.MessgeEvent;  
  21.   
  22.   
  23.   
  24. public class MyExpandableAdapter extends BaseExpandableListAdapter {  
  25.     private List<GoodsBean.DataBean> groupdata;  
  26.     private Context context;  
  27.     private List<List<GoodsBean.DataBean.ListBean>> childlist;  
  28.   
  29.     public MyExpandableAdapter(List<GoodsBean.DataBean> groupdata, Context context, List<List<GoodsBean.DataBean.ListBean>> childlist) {  
  30.         this.groupdata = groupdata;  
  31.         this.context = context;  
  32.         this.childlist = childlist;  
  33.     }  
  34.   
  35.     @Override  
  36.     public int getGroupCount() {  
  37.         return groupdata.size();  
  38.     }  
  39.   
  40.     @Override  
  41.     public int getChildrenCount(int groupPosition) {  
  42.         return childlist.get(groupPosition).size();  
  43.     }  
  44.   
  45.     @Override  
  46.     public Object getGroup(int groupPosition) {  
  47.         return groupdata.get(groupPosition);  
  48.     }  
  49.   
  50.     @Override  
  51.     public Object getChild(int groupPosition, int childPosition) {  
  52.         return null;  
  53.     }  
  54.   
  55.     @Override  
  56.     public long getGroupId(int groupPosition) {  
  57.         return groupPosition;  
  58.     }  
  59.   
  60.     @Override  
  61.     public long getChildId(int groupPosition, int childPosition) {  
  62.         return childPosition;  
  63.     }  
  64.   
  65.     @Override  
  66.     public boolean hasStableIds() {  
  67.         return false;  
  68.     }  
  69.   
  70.     @Override  
  71.     public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  
  72.         final GroupViewHolder holder;  
  73.         if (convertView == null) {  
  74.             convertView = View.inflate(context, R.layout.groupitem, null);  
  75.             holder = new GroupViewHolder();  
  76.             holder.tv_group = convertView.findViewById(R.id.tv_group);  
  77.             holder.group_cb = convertView.findViewById(R.id.gx_group);  
  78.   
  79.             convertView.setTag(holder);  
  80.         } else {  
  81.             holder = (GroupViewHolder) convertView.getTag();  
  82.         }  
  83.         final GoodsBean.DataBean dataBean = groupdata.get(groupPosition);  
  84.         String sellerName = dataBean.getSellerName();  
  85.         holder.group_cb.setChecked(dataBean.isCheck());  
  86.         holder.tv_group.setText(sellerName);  
  87.         //点击一级checkbox的状态  
  88.         holder.group_cb.setOnClickListener(new View.OnClickListener() {  
  89.             @Override  
  90.             public void onClick(View v) {  
  91.                 dataBean.setCheck(holder.group_cb.isChecked());  
  92.                 //改变所有孩子的状态  
  93.                   changeChildState(groupPosition,holder.group_cb.isChecked());  
  94.                   EventBus.getDefault().post(jS());  
  95.                 //通过判断一级的checkbox判断全选的状态  
  96.                    changeMianQXstatus(checkGroupAll());  
  97.                 notifyDataSetChanged();  
  98.             }  
  99.         });  
  100.   
  101.   
  102.         return convertView;  
  103.     }  
  104.   
  105.     @Override  
  106.     public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
  107.         final ChildViewHolder holder;  
  108.         if (convertView == null) {  
  109.             convertView = View.inflate(context, R.layout.childitem, null);  
  110.             holder = new ChildViewHolder();  
  111.   
  112.                holder.tv_title=convertView.findViewById(R.id.title);  
  113.                holder.price=convertView.findViewById(R.id.price);                //价格  
  114.                holder.child_cb=convertView.findViewById(R.id.gouxuan_child);   //子勾选  
  115.                holder.add=convertView.findViewById(R.id.jia);     //加加  
  116.                holder.del=convertView.findViewById(R.id.del);      //删除  
  117.               holder.jian=convertView.findViewById(R.id.jian);    //减  
  118.               holder.title_img=convertView.findViewById(R.id.title_img);  
  119.               holder.tv_num=convertView.findViewById(R.id.tv_num);  //数量  
  120.               holder.yanse=convertView.findViewById(R.id.yanse);  
  121.             convertView.setTag(holder);  
  122.         } else {  
  123.             holder = (ChildViewHolder) convertView.getTag();  
  124.         }  
  125.         final GoodsBean.DataBean.ListBean listBean = childlist.get(groupPosition).get(childPosition);  
  126.         int price = (int) listBean.getPrice();  
  127.              holder.child_cb.setChecked(listBean.isCheck());  
  128.         holder.price.setText("¥:"+price+"");  
  129.         String title = listBean.getTitle();  
  130.         holder.tv_title.setText(title);  
  131.         String subhead = listBean.getSubhead();  
  132.         String substring = subhead.substring(110)+".....";  
  133.         holder.yanse.setText(substring);  
  134.         String images = listBean.getImages();  
  135.         String[] split = images.split("\\|");  
  136.         String s = split[1];  
  137.         ImageLoader.getInstance().displayImage(s,holder.title_img);  
  138.            listBean.setCou(1);  
  139.         //给子条目的checkBox点击事件  
  140.         holder.child_cb.setOnClickListener(new View.OnClickListener() {  
  141.             @Override  
  142.             public void onClick(View v) {  
  143.                 listBean.setCheck(holder.child_cb.isChecked());  
  144.   
  145.                 EventBus.getDefault().post(jS());  
  146.                 //如果二级的checkBox选中,就让一级的checkBox勾选  
  147.                 if(holder.child_cb.isChecked()) {  
  148.   
  149.                     if (ischeckAllchild(groupPosition)) {  
  150.                         //改变一级checkBox的状态  
  151.                         changeGroupstatus(groupPosition,true);  
  152.                         //通过判断一级的CheckBox的状态来改变全选的状态  
  153.                          changeMianQXstatus(checkGroupAll());  
  154.   
  155.                     }  
  156.                 }else{  
  157.                     changeGroupstatus(groupPosition,false);  
  158.                     changeMianQXstatus(checkGroupAll());  
  159.                 }  
  160.                 notifyDataSetChanged();  
  161.             }  
  162.   
  163.         });  
  164.   
  165.         //点击加加的功能  
  166.         holder.add.setOnClickListener(new View.OnClickListener() {  
  167.             @Override  
  168.             public void onClick(View v) {  
  169.                 int cou = listBean.getCou();  
  170.                 holder.tv_num.setText(++cou+"");  
  171.                 listBean.setCou(cou);  
  172.                 if(holder.child_cb.isChecked()){  
  173.                     EventBus.getDefault().post(jS());  
  174.                 }  
  175.   
  176.             }  
  177.         });  
  178.         //点击减减的功能  
  179.         holder.jian.setOnClickListener(new View.OnClickListener() {  
  180.             @Override  
  181.             public void onClick(View v) {  
  182.                 int cou = listBean.getCou();  
  183.   
  184.                 if(cou==1) { return;}  
  185.                 holder.tv_num.setText(--cou+"");  
  186.                 listBean.setCou(cou);  
  187.                 if (holder.child_cb.isChecked()) {  
  188.                         EventBus.getDefault().post(jS());  
  189.   
  190.                 }  
  191.             }  
  192.         });  
  193.       //点击删除  
  194.         holder.del.setOnClickListener(new View.OnClickListener() {  
  195.             @Override  
  196.             public void onClick(View v) {  
  197.                 List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(groupPosition);  
  198.                 GoodsBean.DataBean.ListBean remove = listBeen.remove(childPosition);  
  199.                 if(listBeen.size()==0){  
  200.                     childlist.remove(childPosition);  
  201.                     groupdata.remove(groupPosition);  
  202.                 }  
  203.                EventBus.getDefault().post(jS());  
  204.                 notifyDataSetChanged();  
  205.   
  206.             }  
  207.         });  
  208.   
  209.         return convertView;  
  210.     }  
  211.   
  212.     @Override  
  213.     public boolean isChildSelectable(int groupPosition, int childPosition) {  
  214.         return true;  
  215.     }  
  216.   
  217.     class GroupViewHolder {  
  218.         CheckBox group_cb;  
  219.         TextView tv_group;  
  220.     }  
  221.   
  222.     class ChildViewHolder {  
  223.         CheckBox child_cb;  
  224.         TextView tv_title,price,yanse,tv_num;  
  225.         ImageView title_img,del,add,jian;  
  226.   
  227.   
  228.     }  
  229.     /** 
  230.      * 判断全部的二级是CheckBox否选中 
  231.      * 
  232.      */  
  233.     private boolean ischeckAllchild(int g){  
  234.         List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(g);  
  235.         for (int i = 0; i <listBeen.size() ; i++) {  
  236.             GoodsBean.DataBean.ListBean listBean = listBeen.get(i);  
  237.             if(!listBean.isCheck()){  
  238.                 return false;  
  239.             }  
  240.         }  
  241.         return true;  
  242.     }  
  243.   
  244.     /** 
  245.      * 计算数量和价格 
  246.      * 通过遍历一级的得到二级的CheckBox,如果是选择的选中状态的话,就统计它们的总计的数量和价格 
  247.      */  
  248.     private CountAndPrice jS(){  
  249.         int cou=0;  
  250.         int jiage=0;  
  251.         for (int i = 0; i <groupdata.size() ; i++) {  
  252.             for (int j = 0; j <childlist.get(i).size() ; j++) {  
  253.                 GoodsBean.DataBean.ListBean listBean = childlist.get(i).get(j);  
  254.                 if(listBean.isCheck()){  
  255.                     cou+=listBean.getCou();  
  256.                     jiage+=(int)listBean.getPrice()*listBean.getCou();  
  257.                 }  
  258.   
  259.             }  
  260.   
  261.         }  
  262.         CountAndPrice countAndPrice = new CountAndPrice();  
  263.         countAndPrice.setCount(cou);  
  264.         countAndPrice.setPrice(jiage);  
  265.         return countAndPrice;  
  266.     }  
  267.     /** 
  268.      * 改变一级列表的状态 
  269.      */  
  270.     private void changeGroupstatus(int groupPosition,boolean flag ){  
  271.         GoodsBean.DataBean dataBean = groupdata.get(groupPosition);  
  272.         dataBean.setCheck(flag);  
  273.     }  
  274.     /** 
  275.      * 改变全选的状态 
  276.      * 
  277.      */  
  278.     private void changeMianQXstatus(boolean flag){  
  279.         MessgeEvent messgeEvent = new MessgeEvent();  
  280.          messgeEvent.setCheck(flag);  
  281.         EventBus.getDefault().post(messgeEvent);  
  282.     }  
  283.     /** 
  284.      * 判断一级的是否全部选中 
  285.      */  
  286.     private boolean checkGroupAll(){  
  287.         for (int i = 0; i < groupdata.size(); i++) {  
  288.             if(!groupdata.get(i).isCheck()){  
  289.                 return false;  
  290.             }  
  291.         }  
  292.         return true;  
  293.     }  
  294.     /** 
  295.      * 改变所有孩子的状态 
  296.      */  
  297.     private void changeChildState(int grouPistion,boolean flag){  
  298.         List<GoodsBean.DataBean.ListBean> listBeen = childlist.get(grouPistion);  
  299.         for (int i = 0; i < listBeen.size(); i++) {  
  300.             listBeen.get(i).setCheck(flag);  
  301.         }  
  302.     }  
  303.     /** 
  304.      * 改变全选的 
  305.      */  
  306.     public void qx(boolean flag){  
  307.         for (int i = 0; i < groupdata.size(); i++) {  
  308.            changeGroupstatus(i, flag);  
  309.            changeChildState(i, flag);  
  310.         }  
  311.         EventBus.getDefault().post(jS());  
  312.         notifyDataSetChanged();  
  313.     }  
  314. }  

一级列表的布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.    <CheckBox  
  6.        android:id="@+id/gx_group"  
  7.        android:layout_width="wrap_content"  
  8.        android:layout_height="wrap_content" />  
  9.     <TextView  
  10.         android:id="@+id/tv_group"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content" />  
  13. </LinearLayout>  

二级列表的布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.    <TextView  
  6.        android:id="@+id/title"  
  7.        android:layout_marginLeft="20dp"  
  8.        android:layout_width="match_parent"  
  9.        android:layout_height="wrap_content" />  
  10.     <LinearLayout  
  11.         android:orientation="horizontal"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content">  
  14.         <CheckBox  
  15.             android:layout_marginLeft="20dp"  
  16.             android:gravity="center_vertical"  
  17.             android:id="@+id/gouxuan_child"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content" />  
  20.          <ImageView  
  21.              android:gravity="center_vertical"  
  22.              android:id="@+id/title_img"  
  23.              android:layout_width="50dp"  
  24.              android:layout_height="50dp" />  
  25.           <LinearLayout  
  26.               android:orientation="vertical"  
  27.               android:layout_marginLeft="30dp"  
  28.               android:layout_width="wrap_content"  
  29.               android:layout_height="wrap_content">  
  30.               <TextView  
  31.                   android:id="@+id/price"  
  32.                   android:layout_marginLeft="10dp"  
  33.                   android:layout_width="wrap_content"  
  34.                   android:layout_height="wrap_content" />  
  35.               <TextView  
  36.                   android:id="@+id/yanse"  
  37.                   android:layout_marginLeft="10dp"  
  38.                   android:layout_width="wrap_content"  
  39.                   android:layout_height="wrap_content" />  
  40.               <LinearLayout  
  41.                   android:layout_marginLeft="100dp"  
  42.                   android:orientation="horizontal"  
  43.                   android:layout_width="wrap_content"  
  44.                   android:layout_height="wrap_content">  
  45.                   <ImageView  
  46.                       android:id="@+id/jian"  
  47.                       android:layout_width="20dp"  
  48.                       android:layout_height="20dp"  
  49.                       android:src="@drawable/shopcart_minus_grey" />  
  50.   
  51.                   <TextView  
  52.                       android:id="@+id/tv_num"  
  53.                       android:layout_width="wrap_content"  
  54.                       android:layout_height="wrap_content"  
  55.                       android:layout_marginLeft="5dp"  
  56.                       android:background="@drawable/shopcart_add_btn"  
  57.                       android:paddingBottom="2dp"  
  58.                       android:paddingLeft="20dp"  
  59.                       android:paddingRight="20dp"  
  60.                       android:paddingTop="2dp"  
  61.                       android:text="1" />  
  62.   
  63.                   <ImageView  
  64.                       android:id="@+id/jia"  
  65.                       android:layout_width="20dp"  
  66.                       android:layout_height="20dp"  
  67.                       android:layout_marginLeft="5dp"  
  68.                       android:src="@drawable/shopcart_add_red" />  
  69.               </LinearLayout>  
  70.   
  71.           </LinearLayout>  
  72.                <ImageView  
  73.                    android:id="@+id/del"  
  74.                    android:layout_marginLeft="20dp"  
  75.                    android:background="@drawable/a"  
  76.                    android:layout_width="32dp"  
  77.                    android:layout_height="29dp" />  
  78.     </LinearLayout>  
  79. </LinearLayout>  

画椭圆

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <corners android:radius="200dp"></corners>  
  5.     <stroke android:color="@color/colorPrimaryDark" android:width="1dp"></stroke>  
  6. </shape>  


猜你喜欢

转载自blog.csdn.net/jun_tong/article/details/80752278