购物车加减全反反选逻辑代码

//Activity   首页v层代码
 
public class Show extends Activity implements Ishow,View.OnClickListener{

    private ExpandableListView exlistview;
    private TextView sum;
    private CheckBox qf;
    private Good_bean good_bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        exlistview = findViewById(R.id.exlistview);
        sum = findViewById(R.id.sum);
        qf = findViewById(R.id.qf);
        qf.setOnClickListener(this);
        Myshowgood myshowgood=new Myshowgood();
        myshowgood.showgoodsshop(this,this,new Mygoodshop());

    }

    @Override
    public void mysetadapter(Context context, Good_bean good_bean) {
        List<Good_bean.DataBean> list = good_bean.getData();
        Myadapter myadapter=new Myadapter(context,good_bean,list,sum);
        exlistview.setAdapter(myadapter);
        //获取购物车对象  全选反选时用
        this.good_bean=good_bean;

      /*  //将expanablelistview全部展开
        int groupCount = myadapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            exlistview.expandGroup(i);
        }*/
    }

    @Override
    public void suan(TextView sum,double count) {
        sum.setText("总价:"+count);

    }

    @Override
    public void onClick(View v) {
        //获取购物车对象
        List<Good_bean.DataBean> data = good_bean.getData();
        for (int i = 0; i <data.size() ; i++) {
            //设置父控件取反
            data.get(i).setPrentflog(!data.get(i).getPrentflog());
            //获取子控件
            List<Good_bean.Mygood> list = data.get(i).getList();
            for (int j = 0; j <list.size() ; j++) {
                //设置子控件取反
                list.get(j).setChildselect(!list.get(j).getChildselect());
            }
        }

        //最后调用计算价格的方法   p层的方法   sum为总价的id
     Myshowgood myshowgood=new Myshowgood();
        myshowgood.textcount(new Show(),new Mygoodshop(),good_bean,sum);
             //刷新适配器
         myadapter.notifyDataSetChanged();
}}
 
 

//适配器的代码
 
 
public class Myadapter extends BaseExpandableListAdapter {
    private Context context;
    private Good_bean good_bean;
    private List<Good_bean.DataBean> list;
     private TextView sum;

    public Myadapter(Context context, Good_bean good_bean, List<Good_bean.DataBean> list,TextView sum) {
        this.context = context;
        this.good_bean = good_bean;
        this.list = list;
        this.sum=sum;
    }



    @Override
    public int getGroupCount() {

        return list.size();
    }

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

    @Override
    public Object getGroup(int groupPosition) {

        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().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) {

        Myprenthodler myprenthodler;
        if (convertView==null){
            convertView = View.inflate(context, R.layout.show_prent, null);
            CheckBox prent_ck = convertView.findViewById(R.id.prent_ck);
            TextView prent_tv = convertView.findViewById(R.id.prent_tv);
            myprenthodler = new Myprenthodler(prent_ck, prent_tv);
            convertView.setTag(myprenthodler);
        }else {
            myprenthodler= (Myprenthodler) convertView.getTag();
        }

        myprenthodler.getPrent_tv().setText(list.get(groupPosition).getSellerName());
        Good_bean.DataBean dataBean = list.get(groupPosition);

        myprenthodler.getPrent_ck().setChecked(dataBean.getPrentflog());

        //父控件的点击事件
        myprenthodler.getPrent_ck().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取父控件  自己定义的布尔值
                boolean isCheked = list.get(groupPosition).getPrentflog();
                //点击的时候置反
                isCheked=!isCheked;

                //设置父控件   点击置反
                list.get(groupPosition).setPrentflog(isCheked);

                //获取子控件的集合
                    List<Good_bean.Mygood> list = Myadapter.this.list.get(groupPosition).getList();
                    for (int i = 0; i < list.size(); i++) {
                        //设置子控件和父父控件相同的布尔值
                        list.get(i).setChildselect(isCheked);

                    //通知界面刷新
                    notifyDataSetChanged();

                    //调用计算的方法
                        Myshowgood myshowgood=new Myshowgood();
                        myshowgood.textcount(new Show(),new Mygoodshop(),good_bean,sum);
                }
            }
        });


        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final Mychildhodler mychildhodler;
        if (convertView==null){
           convertView = View.inflate(context, R.layout.show_chiled, null);
           CheckBox child_ck = convertView.findViewById(R.id.child_ck);
          TextView cjild_name = convertView.findViewById(R.id.cjild_name);
           TextView cjild_price = convertView.findViewById(R.id.cjild_price);
            SimpleDraweeView child_sd = convertView.findViewById(R.id.child_sd);

          ImageView jian = convertView.findViewById(R.id.jian);
           ImageView jia = convertView.findViewById(R.id.jia);
          EditText ed = convertView.findViewById(R.id.ed);

           mychildhodler = new Mychildhodler(child_ck,cjild_name,cjild_price,child_sd,ed,jia,jian);
           convertView.setTag(mychildhodler);
       }else {
           mychildhodler= (Mychildhodler) convertView.getTag();
       }
       //获取图片的uri   拆分取第一个
        String images = list.get(groupPosition).getList().get(childPosition).getImages();
        String[] split = images.split(".jpg");
        mychildhodler.cjild_name.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
       mychildhodler.cjild_price.setText(list.get(groupPosition).getList().get(childPosition).getPrice()+"");
       //拼接字符串为图片赋值
        mychildhodler.getChild_sd().setImageURI(split[0]+".jpg");
        //为复选框设置 自己定义的布尔值
        mychildhodler.child_ck.setChecked(list.get(groupPosition).getList().get(childPosition).getChildselect());

        //为文本框赋值
        Good_bean.Mygood mygood = list.get(groupPosition).getList().get(childPosition);
        //自己定义属性count默认为0   赋值时需要+1
        mychildhodler.ed.setText(mygood.getCount()+1+"");

        //自控件的点击事件
        mychildhodler.child_ck.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                //获取自定义子控件的布尔值
                boolean isCheked = list.get(groupPosition).getList().get(childPosition).getChildselect();
                //点击时置反
                isCheked=!isCheked;

                //给子控件复选框赋值  目的为了记录复选框的值
                list.get(groupPosition).getList().get(childPosition).setChildselect(isCheked);

                //当前类名调出  子控件的集合
                List<Good_bean.Mygood> list1 = Myadapter.this.list.get(groupPosition).getList();
                //设置标识位
                boolean flag = true;
                //遍历子控件的集合
               for (int i = 0; i < list1.size(); i++) {
                   //如果子控件有一个的值为false时  父控件的值为false  否则不走if判断  父控件为true
                    if (!list1.get(i).getChildselect()){
                        flag=false;
                    }
                }
             //设置父控件的值
                Myadapter.this.list.get(groupPosition).setPrentflog(flag);


                //通知改变
                notifyDataSetChanged();


                //调用p层计算的方法    把购车车对象传过去  便于获取数据
                //计算的逻辑在m层做  返回一个double  在p层调用v层的方法把m层计算好的值赋值给textview
                Myshowgood myshowgood=new Myshowgood();
                myshowgood.textcount(new Show(),new Mygoodshop(),good_bean,sum);


            }
        });


       //加减的点击事件     加的事件
        mychildhodler.getJia().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 //获取自定义的int类型的count 设置每点击加号的时候count加1
                int count = list.get(groupPosition).getList().get(childPosition).getCount();
                list.get(groupPosition).getList().get(childPosition).setCount(++count);

                //调用p层的计算方法
                Myshowgood myshowgood=new Myshowgood();
     
                myshowgood.textcount(new Show(),new Mygoodshop(),good_bean,sum);
                notifyDataSetChanged();

            }
        });

        //点击减的事件
        mychildhodler.getJian().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 //获取自定义的int类型的count值
                int count = list.get(groupPosition).getList().get(childPosition).getCount();
                //如果值>0的话  每次点击时  减减
                if (count>0) {
                    count--;
                    list.get(groupPosition).getList().get(childPosition).setCount(count);
                }else{
                    Toast.makeText(context,"不能再减了",Toast.LENGTH_SHORT).show();
                }
                //调用计算的方法  p层
                Myshowgood myshowgood=new Myshowgood();
                myshowgood.textcount(new Show(),new Mygoodshop(),good_bean,sum);
                notifyDataSetChanged();
            }
        });

        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    //子控件的优化类   set  get   有参构造
    class Mychildhodler{
        private CheckBox child_ck;
        private TextView cjild_name;
        private TextView cjild_price;
        private  SimpleDraweeView child_sd;
        private  EditText ed;
        private ImageView jia;
        private ImageView jian;

        public Mychildhodler(CheckBox child_ck, TextView cjild_name, TextView cjild_price, SimpleDraweeView child_sd, EditText ed, ImageView jia, ImageView jian) {
            this.child_ck = child_ck;
            this.cjild_name = cjild_name;
            this.cjild_price = cjild_price;
            this.child_sd = child_sd;
            this.ed = ed;
            this.jia = jia;
            this.jian = jian;
        }

        public ImageView getJia() {
            return jia;
        }

        public void setJia(ImageView jia) {
            this.jia = jia;
        }

        public ImageView getJian() {
            return jian;
        }

        public void setJian(ImageView jian) {
            this.jian = jian;
        }

        public EditText getEd() {
            return ed;
        }

        public void setEd(EditText ed) {
            this.ed = ed;
        }

        public Mychildhodler(CheckBox child_ck, TextView cjild_name, TextView cjild_price, SimpleDraweeView child_sd, EditText ed) {
            this.child_ck = child_ck;
            this.cjild_name = cjild_name;
            this.cjild_price = cjild_price;
            this.child_sd = child_sd;
            this.ed = ed;
        }

        public CheckBox getChild_ck() {
            return child_ck;
        }

        public void setChild_ck(CheckBox child_ck) {
            this.child_ck = child_ck;
        }

        public TextView getCjild_name() {
            return cjild_name;
        }

        public void setCjild_name(TextView cjild_name) {
            this.cjild_name = cjild_name;
        }

        public TextView getCjild_price() {
            return cjild_price;
        }

        public void setCjild_price(TextView cjild_price) {
            this.cjild_price = cjild_price;
        }

        public SimpleDraweeView getChild_sd() {
            return child_sd;
        }

        public void setChild_sd(SimpleDraweeView child_sd) {
            this.child_sd = child_sd;
        }


    }

    //父控件的优化类   set  get   有参构造
    class Myprenthodler{
        private CheckBox prent_ck;
        private TextView prent_tv;


        public CheckBox getPrent_ck() {
            return prent_ck;
        }

        public void setPrent_ck(CheckBox prent_ck) {
            this.prent_ck = prent_ck;
        }

        public TextView getPrent_tv() {
            return prent_tv;
        }

        public void setPrent_tv(TextView prent_tv) {
            this.prent_tv = prent_tv;
        }

        public Myprenthodler(CheckBox prent_ck, TextView prent_tv) {

            this.prent_ck = prent_ck;
            this.prent_tv = prent_tv;
        }
    }
}


//p层计算的方法
 
public class Myshowgood implements Ipshowgood {
 
//计算的方法   sum为计算文本的id      good_bean为v层传过来的购物车数据的对象  sum为总价的id
@Override
public void textcount(Ishow ishow, Igoodshop igoodshop, Good_bean good_bean, TextView sum) {

   //调用m层计算的方法   返回结果值
    double jisuan = igoodshop.jisuan(good_bean);
    ishow.suan(sum,jisuan);
}
 


//m层计算的方法
//计算的方法
@Override
public double jisuan(Good_bean good_bean) {
    //初始化变量
    double count = 0;
    //获取父控件的集合
    List<Good_bean.DataBean> data = good_bean.getData();
    //遍历获取子控件的集合
    for (int i = 0; i <data.size() ; i++) {
        List<Good_bean.Mygood> list = data.get(i).getList();
        //遍历获取子控件的价格
        for (int j = 0; j < list.size(); j++) {

            //如果复选框为true时   计算
            if (list.get(j).getChildselect()) {
                 //默认为0  所以必须加1
                int count1 = list.get(j).getCount()+1;
                Log.d("计算", "jisuan: "+count1);
                double price = list.get(j).getPrice()*count1;
                count += price;
            }
        }
    }
    return count;
}



  <!--去除指针-->
    <ExpandableListView
        android:groupIndicator="@null"
        android:id="@+id/exlistview"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"
        ></ExpandableListView>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值