购物车

1.布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/linearLayout">
        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:groupIndicator="@null"
            android:id="@+id/ev">
        </ExpandableListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout">
        <CheckBox
            android:layout_width="0dp"
            android:text="全选"
            android:layout_weight="1"
            android:checked="true"
            android:layout_height="wrap_content"
            android:id="@+id/cb_all"/>
        <TextView
            android:layout_width="0dp"
            android:gravity="right"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="总价:"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="0.0"
            android:id="@+id/text_sum"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="删除"
            android:id="@+id/btn_del"/>
    </LinearLayout>
</RelativeLayout>
2.主页

public class MainActivity extends AppCompatActivity implements IView{

    private ExpandableListView ev;
    public CheckBox cb;
    public TextView sum;
    private Button delete;
    private MyPresenter presenter;
    private List<fatherBean> list;
    private List<List<childBean>> lists;
    private CarAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ev = (ExpandableListView) findViewById(R.id.ev);
        cb = (CheckBox)  findViewById(R.id.cb_all);
        sum = (TextView)  findViewById(R.id.text_sum);
        delete = (Button) findViewById(R.id.btn_del);

        list=new ArrayList<>();
        lists=new ArrayList<>();

        //新建适配器
        adapter = new CarAdapter(this, list, lists,this);
        ev.setAdapter(adapter);

        //全选按钮点击事件
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int i=0;i<list.size();i++){
                    list.get(i).setIsflag(cb.isChecked());
                }
                for(int i=0;i<lists.size();i++){
                    for(int j=0;j<lists.get(i).size();j++){
                        lists.get(i).get(j).setIsflag(cb.isChecked());
                    }
                }
                int n=0;
                for (int j=0;j<lists.size();j++){
                    List<childBean> beanList = lists.get(j);
                    for (int k=0;k<beanList.size();k++){
                        if(beanList.get(k).isflag()){
                            int num = beanList.get(k).getNum();
                            double price1 = beanList.get(k).getPrice();
                            n+=price1*num;
                        }
                    }
                }
                sum.setText("总价为:"+n);
                //刷新适配器
                adapter.notifyDataSetChanged();
            }
        });

        //删除按钮点击事件
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int i=0;i<lists.size();i++){
                    List<childBean> beanList = lists.get(i);
                    for (int j=0;j<beanList.size();j++){
                        childBean bean = beanList.get(j);
                        if (bean.isflag()){
                            String pid = bean.getPid();
                            lists.get(i).remove(j);
                            Map<String,String> map1=new HashMap<String, String>();
                            map1.put("uid","4479");
                            map1.put("pid",pid);
                            presenter.get("http://120.27.23.105/",map1,"del");
                            mouth(MainActivity.this);
                        }
                    }
                    if (lists.get(i).size()==0){
                        lists.remove(i);
                        list.remove(i);
                        i--;
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });

        presenter = new MyPresenter(this);
        Map<String,String> map=new HashMap<>();
        map.put("source","android");
        map.put("uid","4479");
        presenter.get("http://120.27.23.105/",map,"getcars");
    }

    //判断是否全选
    public boolean setcheck(){
        boolean b=true;
        for (int i=0;i<list.size();i++){
            if (!list.get(i).isflag()){
                b=list.get(i).isflag();
                break;
            }
            List<childBean> beanList = lists.get(i);
            for(int j=0;j<beanList.size();j++){
                if (!beanList.get(j).isflag()){
                    b=beanList.get(j).isflag();
                    break;
                }
            }
        }
        return b;
    }

    //计算总价的方法
    public void mouth(MainActivity f){
        float n=0;
        for (int j=0;j<lists.size();j++){
            List<childBean> beanList = lists.get(j);
            for (int k=0;k<beanList.size();k++){
                if(beanList.get(k).isflag()){
                    int num = beanList.get(k).getNum();
                    double price1 = beanList.get(k).getPrice();
                    n+=price1*num;
                }
            }
        }
        f.sum.setText(""+n);
    }

    @Override
    public void onSuccess(Object o, String tag) {
        if (tag.equals("getcars")){
            if (o!=null){
                CarBean a = (CarBean)o;
                List<CarBean.DataBean> beanList = a.getData();
                //建立数据源
                for(int i=0;i<beanList.size();i++){
                    String name = beanList.get(i).getSellerName();
                    list.add(new fatherBean(name,true));
                    List<CarBean.DataBean.ListBean> listBeen = beanList.get(i).getList();
                    List<childBean> l=new ArrayList<>();
                    for(int j=0;j<listBeen.size();j++){
                        double price = listBeen.get(j).getPrice();
                        String s = listBeen.get(j).getImages();
                        String[] strings = s.split("!");
                        String title = listBeen.get(j).getTitle();
                        int id = listBeen.get(j).getPid();
                        int num = listBeen.get(j).getNum();
                        l.add(new childBean(title,strings[0],id+"",price,true,num));
                    }
                    lists.add(l);
                }
                //默认展开
                for(int s = 0; s < adapter.getGroupCount(); s++){
                    ev.expandGroup(s);
                }
                mouth(this);
                //刷新适配器
                adapter.notifyDataSetChanged();
            }
        }else if(tag.equals("del")){
            delBean bean = (delBean) o;
        }
    }

    @Override
    public void onFailed(Exception e) {

    }
}

3.加减器布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/down"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/delete"/>
    <View
        android:id="@+id/view"
        android:layout_width="20dp"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="#5c595c"
        android:layout_toRightOf="@id/delete"/>
    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:layout_toRightOf="@id/delete"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"/>
    <View
        android:layout_width="20dp"
        android:layout_height="1dp"
        android:background="#5c595c"
        android:layout_toRightOf="@id/delete"
        android:layout_marginTop="23dp"/>
    <ImageView
        android:id="@+id/add"
        android:src="@drawable/up"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/view"
        android:layout_toEndOf="@+id/view"  />
</RelativeLayout>

4.加减器

public class AddView extends RelativeLayout{

    private TextView count;

    public AddView(Context context) {
        this(context,null);
    }

    public AddView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private OnAddDeleteClickListener listener;

    //对外提供一个点击的回调接口
    public interface OnAddDeleteClickListener{
        void onAddClick(View v);
        void onDelClick(View v);
    }

    public void setOnAddDeleteClick(OnAddDeleteClickListener listener){
        this.listener = listener;
    }

    private void initView(final Context context, AttributeSet attrs, int defStyleAttr) {
        View view = View.inflate(context, R.layout.view_add, this);
        ImageView delete = (ImageView)view.findViewById(R.id.delete);
        count = (TextView)view.findViewById(R.id.count);
        ImageView add = (ImageView)view.findViewById(R.id.add);
        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDelClick(view);
            }
        });
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onAddClick(view);
            }
        });
    }

    //对外提供设置EditText值的方法
    public void setNumber(int number){
        count.setText(number + "");
    }
    //得到控件原来的值,并转成int类型
    public int getNumber(){
        int number = Integer.parseInt(count.getText().toString().trim());
        return number;
    }
}
5.父级bean类

public class fatherBean {
    private String name;
    private boolean isflag;

    public fatherBean(String name, boolean isflag) {
        this.name = name;
        this.isflag = isflag;
    }

    public boolean isflag() {
        return isflag;
    }

    public void setIsflag(boolean isflag) {
        this.isflag = isflag;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


6.子级bean类

public class childBean {
    private String name;
    private String imgurl;
    private String pid ;
    private boolean isflag;
    private double price;
    private int num;

    public childBean(String name, String imgurl, String pid, double price, boolean isflag,int num) {
        this.name = name;
        this.imgurl = imgurl;
        this.pid = pid;
        this.isflag = isflag;
        this.price = price;
        this.num=num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getPrice() {
        return price;
    }

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

    public boolean isflag() {
        return isflag;
    }

    public void setIsflag(boolean isflag) {
        this.isflag = isflag;
    }

    public childBean(String name, String imgurl, String pid) {
        this.name = name;
        this.imgurl = imgurl;
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImgurl() {
        return imgurl;
    }

    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }
}

7.删除 添加bean类

public class delBean {
    private String msg;
    private String code;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

8.适配器

public class CarAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<fatherBean> list;
    private List<List<childBean>> lists;
    private MainActivity f;

    public CarAdapter(Context context, List<fatherBean> list, List<List<childBean>> lists,MainActivity f) {
        this.context = context;
        this.list = list;
        this.lists = lists;
        this.f=f;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return lists.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return list.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return lists.get(i).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final fatherBean fatherbean = list.get(i);
        final List<childBean> childbeen = lists.get(i);
        view = View.inflate(this.context, R.layout.view_father, null);
        TextView textView = (TextView) view.findViewById(R.id.title_father_car);
        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_father_car);
        textView.setText(fatherbean.getName());
        checkBox.setChecked(fatherbean.isflag());
        //复选框点击事件
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = checkBox.isChecked();
                fatherbean.setIsflag(checked);
                for (int j=0;j<childbeen.size();j++){
                    childbeen.get(j).setIsflag(checked);
                }
                //计算总价方法
                f.mouth(f);
                boolean setcheck = f.setcheck();
                f.cb.setChecked(setcheck);
                notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final childBean bean = lists.get(i).get(i1);
        //加载布局
        view= View.inflate(context, R.layout.view_child,null);
        //获取构件
        SimpleDraweeView img = (SimpleDraweeView) view.findViewById(R.id.child_img);
        TextView title = (TextView) view.findViewById(R.id.child_tit);
        AddView addView = (AddView) view.findViewById(R.id.addview);
        final TextView price = (TextView) view.findViewById(R.id.child_price);
        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_child);
        //修改内容
        addView.setNumber(bean.getNum());
        price.setText(bean.getPrice()+"");
        title.setText(bean.getName());
        checkBox.setChecked(lists.get(i).get(i1).isflag());
        img.setImageURI(bean.getImgurl());
        //加减器点击事件
        addView.setOnAddDeleteClick(new AddView.OnAddDeleteClickListener() {
            @Override
            public void onAddClick(View v) {
                int num = bean.getNum();
                num++;
                bean.setNum(num);
                //计算总价方法
                f.mouth(f);
                notifyDataSetChanged();
            }
            @Override
            public void onDelClick(View v) {
                int num = bean.getNum();
                if (num==1){
                    Toast.makeText(context,"数量不能小于1",Toast.LENGTH_SHORT).show();
                }
                if(num>1){
                    num--;
                }
                bean.setNum(num);
                //计算总价方法
                f.mouth(f);
                notifyDataSetChanged();
            }
        });

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = checkBox.isChecked();
                lists.get(i).get(i1).setIsflag(checked);
                boolean b=true;
                if (checked){
                    for (int j=0;j<lists.get(i).size();j++){
                        boolean flag = lists.get(i).get(j).isflag();
                        if (flag==false){
                            b=false;
                        }
                    }
                    //修改父级的选中状态
                    if (b){
                        list.get(i).setIsflag(true);
                    }
                }else{
                    list.get(i).setIsflag(false);
                }
                //计算总价方法
                f.mouth(f);
                boolean setcheck = f.setcheck();
                f.cb.setChecked(setcheck);
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
}


父级布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:id="@+id/cb_father_car"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xxx"
            android:id="@+id/title_father_car"/>
    </LinearLayout>
</LinearLayout>


子级布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:Fresco="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/cb_child"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="35dp" />

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/child_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            Fresco:placeholderImage="@mipmap/ic_launcher_round"
            Fresco:failureImage="@mipmap/ic_launcher_round"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="xxx"
                android:id="@+id/child_tit"/>
            <LinearLayout
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="xxx"
                    android:id="@+id/child_price"/>
                <com.example.bwshop.view.Addview
                    android:id="@+id/addview"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content">

                </com.example.bwshop.view.Addview>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值