shopping

来源:互联网 发布:程序员优秀简历 编辑:程序博客网 时间:2024/05/23 10:28

//XML布局框架

<TextView    android:text="购物车"    android:textSize="30dp"    android:background="#00f"    android:layout_marginLeft="275dp"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /><ExpandableListView    android:id="@+id/expandablelistview_show"    android:layout_width="match_parent"    android:layout_height="900dp"    android:layout_weight="1"    android:scrollbars="none"    android:divider="@null"    ></ExpandableListView><LinearLayout    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="50dp">    <CheckBox        android:id="@+id/cb_all"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginLeft="100dp"        android:orientation="vertical"        >        <TextView            android:id="@+id/tv_all_price"            android:textSize="20dp"            android:text="合计:¥0.0"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/tv_all_geshu"            android:text="总计:0个商品"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <Button        android:id="@+id/bt_jeisuan"        android:text="去结算"        android:textSize="20dp"        android:background="#f00"        android:layout_marginLeft="20dp"        android:layout_width="100dp"        android:layout_height="wrap_content" /></LinearLayout>
//XML布局复选框
<CheckBox    android:id="@+id/store_cb"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /><TextView    android:id="@+id/store_name_show"    android:text="AK"    android:textSize="30dp"    android:layout_marginLeft="50dp"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />
//XML布局子条目
<CheckBox    android:id="@+id/cb_commodity_show"    android:layout_gravity="center"    android:layout_marginLeft="20dp"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /><LinearLayout    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <TextView        android:id="@+id/commodity_title"        android:layout_marginTop="20dp"        android:textStyle="bold"        android:text="标题"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <LinearLayout        android:orientation="horizontal"        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <ImageView            android:id="@+id/commodity_img"            android:src="@mipmap/ic_launcher"            android:layout_marginTop="30dp"            android:layout_width="80dp"            android:layout_height="80dp" />        <RelativeLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <TextView                android:id="@+id/commodity_price"                android:text="¥:20"                android:textColor="#f00"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />            <TextView                android:id="@+id/commodity_color"                android:layout_marginTop="10dp"                android:text="颜色"                android:layout_below="@id/commodity_price"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />            <TextView                android:id="@+id/commodity_size"                android:layout_toRightOf="@id/commodity_color"                android:layout_marginTop="10dp"                android:text="尺寸"                android:layout_below="@id/commodity_price"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />            <LinearLayout                android:layout_below="@id/commodity_color"                android:orientation="horizontal"                android:layout_width="wrap_content"                android:layout_height="wrap_content">                <Button                    android:id="@+id/commodity_geshu_down"                    android:text="-"                    android:layout_width="30dp"                    android:layout_height="wrap_content" />                <Button                    android:text="1"                    android:id="@+id/commodity_geshu_show"                    android:layout_width="50dp"                    android:layout_height="wrap_content" />                <Button                    android:text="+"                    android:id="@+id/commodity_geshu_up"                    android:layout_width="30dp"                    android:layout_height="wrap_content" />            </LinearLayout>        </RelativeLayout>        <Button            android:id="@+id/commodity_delete"            android:text="删除"            android:layout_gravity="center"            android:layout_marginLeft="180dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>
//MainActivity
public class Main2Activity extends AppCompatActivity implements View.OnClickListener,ShowView,GroupCheckBox,ChiledCheckBox,UpGeshu,DownGeshu,Delete {    List<ShowBean.DataBean> data;    ExpandableListView exdblv;    ExpandableListViewAdpt ExdblvAdpt;    ShowPresaenter onePresenter;    List<IsnoBean> GroupIsno;    List<List<IsnoBean>> ChiledIsno;    CheckBox CbAll;    TextView TvAllPrice;    TextView TvAllGeshu;    Button BtnJiesuan;    int allprice = 0;    int allgeshu = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        data=new ArrayList<ShowBean.DataBean>();        exdblv = (ExpandableListView) findViewById(R.id.expandablelistview_show);        CbAll = (CheckBox) findViewById(R.id.cb_all);        TvAllPrice = (TextView) findViewById(R.id.tv_all_price);        TvAllGeshu = (TextView) findViewById(R.id.tv_all_geshu);        BtnJiesuan = (Button) findViewById(R.id.bt_jeisuan);        //P层        onePresenter=new ShowPresaenter();        onePresenter.attachView(this);        onePresenter.getNews();        //总的复选框点击事件        CbAll.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch(view.getId()){            case R.id.cb_all:                boolean checked = CbAll.isChecked();                for(int i=0;i<GroupIsno.size();i++){                    GroupIsno.get(i).setIsno(checked);                    List<IsnoBean> isnoBeen = ChiledIsno.get(i);                    for(int j=0;j<isnoBeen.size();j++){                        isnoBeen.get(j).setIsno(checked);                    }                }                ExdblvAdpt.notifyDataSetChanged();                ZongjiaAndGeshu();                break;        }    }    @Override    public void onDelete(int i, int i1) {        data.get(i).getList().remove(i1);        ChiledIsno.get(i).remove(i1);        GaiGroupChexkBox(i);        DeleteDianpu(i);        ZongjiaAndGeshu();    }    @Override    public void onUpGeshu(int num, int i, int i1) {        data.get(i).getList().get(i1).setNum(num);        ExdblvAdpt.notifyDataSetChanged();        ZongjiaAndGeshu();    }    @Override    public void onDownGeshu(int num, int i, int i1) {        data.get(i).getList().get(i1).setNum(num);        if(data.get(i).getList().get(i1).getNum()==0){            data.get(i).getList().remove(i1);            ChiledIsno.get(i).remove(i1);        }        GaiGroupChexkBox(i);        DeleteDianpu(i);        ZongjiaAndGeshu();    }    @Override    public void onGroupBox(boolean isno, int i) {        GroupIsno.get(i).setIsno(isno);        List<IsnoBean> isnoBeen = ChiledIsno.get(i);        for(int u=0;u<isnoBeen.size();u++){            isnoBeen.get(u).setIsno(isno);        }        ExdblvAdpt.notifyDataSetChanged();        AllCheckBox();        ZongjiaAndGeshu();    }    @Override    public void onChiledBox(boolean isno, int i, int i1) {        ChiledIsno.get(i).get(i1).setIsno(isno);        GaiGroupChexkBox(i);        ExdblvAdpt.notifyDataSetChanged();        AllCheckBox();        ZongjiaAndGeshu();    }    @Override    public void ShowSuccess(String tag, List<ShowBean.DataBean> showList) {        data.addAll(showList);        //复选框选择状态的集合        GroupIsno = new ArrayList<IsnoBean>();        ChiledIsno = new ArrayList<List<IsnoBean>>();        for(int i=0;i<data.size();i++){            GroupIsno.add(new IsnoBean(false));            List<IsnoBean> li = new ArrayList<IsnoBean>();            for(int j=0;j<data.get(i).getList().size();j++){                li.add(new IsnoBean(false));            }            ChiledIsno.add(li);        }        //适配器        ExdblvAdpt = new ExpandableListViewAdpt(this,data,GroupIsno,ChiledIsno);        ExdblvAdpt.setOnGroupCheckBox(this);        ExdblvAdpt.setOnChiledCheckBox(this);        ExdblvAdpt.setOnUpGeshu(this);        ExdblvAdpt.setOnDownGeshu(this);        ExdblvAdpt.setOnDelete(this);        //配置适配器        exdblv.setAdapter(ExdblvAdpt);        int groupCount = exdblv.getCount();        for (int i=0; i<groupCount; i++) {            exdblv.expandGroup(i);        };        exdblv.setGroupIndicator(null);    }    @Override    public void ShowFailed(String tag, Exception e) {    }    //算总价格和总个数---每次点击事件完之后都调用此方法    public void ZongjiaAndGeshu(){        allprice = 0;        allgeshu = 0;        for(int i=0;i<ChiledIsno.size();i++){            List<IsnoBean> isnoBeen = ChiledIsno.get(i);            for(int j=0;j<isnoBeen.size();j++){                ShowBean.DataBean.ListBean listBean = data.get(i).getList().get(j);                if(isnoBeen.get(j).getIsno()){                    allprice+=(int)(listBean.getNum()*listBean.getBargainPrice());                    allgeshu+=listBean.getNum();                }            }        }        TvAllPrice.setText("总价:¥"+allprice);        TvAllGeshu.setText("共"+allgeshu+"件商品");    }    //根据所有GroupCheckBox的状态,改变总的复选框状态    private void AllCheckBox() {        int e = 0;        for(int r=0;r<GroupIsno.size();r++){            if(!GroupIsno.get(r).getIsno()){                e = 1;            }        }        if(e==0){            CbAll.setChecked(true);        }else{            CbAll.setChecked(false);        }    }    //根据商品的复选框改变店铺的复选框状态    private void GaiGroupChexkBox(int i) {        int t = 0;        for(int p=0;p<ChiledIsno.get(i).size();p++){            if(!ChiledIsno.get(i).get(p).getIsno()){                t = 1;            }        }        if(t==0){            GroupIsno.get(i).setIsno(true);        }else{            GroupIsno.get(i).setIsno(false);        }    }    //删除店铺    private void DeleteDianpu(int i) {        if(data.get(i).getList()==null||data.get(i).getList().size()<1){            data.remove(i);            GroupIsno.remove(i);            ChiledIsno.remove(i);        }        ExdblvAdpt.notifyDataSetChanged();        AllCheckBox();        if(data==null||data.size()<1){            CbAll.setChecked(false);        }    }}
//适配器
public class ExpandableListViewAdpt extends BaseExpandableListAdapter{    Context context;    //店家,商品数据    List<ShowBean.DataBean> data;    //复选框传值集合    List<IsnoBean> GroupIsno;   //Group集合    List<List<IsnoBean>> ChiledIsno;   //Chiled集合    //有参构造    public ExpandableListViewAdpt(Context context, List<ShowBean.DataBean> data, List<IsnoBean> GroupIsno, List<List<IsnoBean>> ChiledIsno) {        this.context = context;        this.data = data;        this.GroupIsno = GroupIsno;        this.ChiledIsno = ChiledIsno;    }    //GroupCheckBox接口    private GroupCheckBox groupcheckbox;    public void setOnGroupCheckBox(GroupCheckBox groupcheckbox){        this.groupcheckbox = groupcheckbox;    }    //ChiledCheckBox接口    private ChiledCheckBox chiledcheckbox;    public void setOnChiledCheckBox(ChiledCheckBox chiledcheckbox){        this.chiledcheckbox = chiledcheckbox;    }    //加商品个数的接口    private UpGeshu upgeshu;    public void setOnUpGeshu(UpGeshu upgeshu){        this.upgeshu = upgeshu;    }    //减商品个数的接口    private DownGeshu downgeshu;    public void setOnDownGeshu(DownGeshu downgeshu){        this.downgeshu = downgeshu;    }    //删除商品的接口    private Delete delete;    public void setOnDelete(Delete delete){        this.delete = delete;    }    @Override    public int getGroupCount() {        return data.size();    }    @Override    public int getChildrenCount(int groupPosition) {        return data.get(groupPosition).getList().size();    }    @Override    public Object getGroup(int groupPosition) {        return data.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return data.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 view, ViewGroup parent) {        final ShowBean.DataBean dataBean = data.get(groupPosition);        final IsnoBean isnoBean = GroupIsno.get(groupPosition);        final ViewHodler1 vh1;        if(view==null){            vh1 = new ViewHodler1();            view = View.inflate(context, R.layout.store_item,null);            vh1.CbStore = (CheckBox) view.findViewById(R.id.store_cb);            vh1.TvStoreName = (TextView) view.findViewById(R.id.store_name_show);            view.setTag(vh1);        }else{            vh1 = (ViewHodler1) view.getTag();        }        vh1.TvStoreName.setText(dataBean.getSellerName());        //复选框        vh1.CbStore.setChecked(isnoBean.getIsno());//false        vh1.CbStore.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                boolean checked = vh1.CbStore.isChecked();                groupcheckbox.onGroupBox(checked,groupPosition);            }        });        //返回当前视图        return view;    }    @Override    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {        //本商品        final ShowBean.DataBean.ListBean listBean = data.get(i).getList().get(i1);        //本商品的复选框的值        final IsnoBean bisnoBean = ChiledIsno.get(i).get(i1);        final ViewHolder2 vh2;        if(view==null){            vh2 = new ViewHolder2();            view = View.inflate(context, R.layout.commodity_item,null);            vh2.CbCommodity = (CheckBox) view.findViewById(R.id.cb_commodity_show);            vh2.ImgCommodityPicture = (ImageView) view.findViewById(R.id.commodity_img);            vh2.TvCommodityTitle = (TextView) view.findViewById(R.id.commodity_title);            vh2.TvCommodityPrice = (TextView) view.findViewById(R.id.commodity_price);            vh2.TvCommodityColor = (TextView) view.findViewById(R.id.commodity_color);            vh2.TvCommoditySize = (TextView) view.findViewById(R.id.commodity_size);            vh2.BtnCommodityUp = (Button) view.findViewById(R.id.commodity_geshu_up);            vh2.BtnCommodityShow = (Button) view.findViewById(R.id.commodity_geshu_show);            vh2.BtnCommodityDown = (Button) view.findViewById(R.id.commodity_geshu_down);            vh2.BtnCommodityDelete = (Button) view.findViewById(R.id.commodity_delete);            view.setTag(vh2);        }else{            vh2 = (ViewHolder2) view.getTag();        }       // String[] split = listBean.getImages().split("\\|");//vh2.ImgCommodityPicture        String images = data.get(i).getList().get(i1).getImages();        String[] split1 = images.split("\\|");        Glide.with(context).load(split1[0]).into(vh2.ImgCommodityPicture);        //ImageLoader.getInstance().displayImage(split[0],vh2.ImgCommodityPicture);        vh2.TvCommodityTitle.setText(listBean.getTitle());        vh2.TvCommodityPrice.setText("¥:"+(int)listBean.getBargainPrice());        vh2.TvCommodityColor.setText("颜色:"+listBean.getPrice());        vh2.TvCommoditySize.setText("尺寸:"+listBean.getPid()+"L");        vh2.BtnCommodityShow.setText(listBean.getNum()+"");        //复选框        vh2.CbCommodity.setChecked(bisnoBean.getIsno());        vh2.CbCommodity.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                boolean checked = vh2.CbCommodity.isChecked();                chiledcheckbox.onChiledBox(checked,i,i1);            }        });        //加个数        vh2.BtnCommodityUp.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String s = vh2.BtnCommodityShow.getText().toString();                int num = Integer.valueOf(s).intValue();                num++;                upgeshu.onUpGeshu(num,i,i1);            }        });        //减个数        vh2.BtnCommodityDown.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String s = vh2.BtnCommodityShow.getText().toString();                int num = Integer.valueOf(s).intValue();                num--;                downgeshu.onDownGeshu(num,i,i1);            }        });        //删除        vh2.BtnCommodityDelete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                delete.onDelete(i,i1);            }        });        //返回当前视图        return view;    }    @Override    public boolean isChildSelectable(int i, int i1) {        return true;    }    class ViewHodler1{        private CheckBox CbStore;        private TextView TvStoreName;    }    class ViewHolder2{        private CheckBox CbCommodity;        private TextView TvCommodityTitle;        private ImageView ImgCommodityPicture;        private TextView TvCommodityPrice;        private TextView TvCommodityColor;        private TextView TvCommoditySize;        private Button BtnCommodityUp;        private Button BtnCommodityShow;        private Button BtnCommodityDown;        private Button BtnCommodityDelete;    }}
//接口
public interface ChiledCheckBox {    void onChiledBox(boolean isno,int i,int i1);}
public interface Delete {    void onDelete(int i,int i1);}
public interface DownGeshu {    void onDownGeshu(int num, int i, int i1);}
public interface GroupCheckBox {    void onGroupBox(boolean isno,int i);}
public interface UpGeshu {    void onUpGeshu(int num,int i,int i1);}

原创粉丝点击