二级购物车+编辑+删除

来源:互联网 发布:android wear源码下载 编辑:程序博客网 时间:2024/05/16 07:07


布局文件(main)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    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">   <RelativeLayout       android:layout_width="match_parent"       android:layout_height="wrap_content">       <TextView           android:layout_width="match_parent"           android:layout_height="40dp"           android:background="#990000ff"           android:gravity="center"           android:text="购物车"           android:textColor="#ff3660"           android:textSize="25sp" />       <TextView           android:id="@+id/bj"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_alignParentEnd="true"           android:layout_alignParentRight="true"           android:layout_centerVertical="true"           android:layout_marginEnd="11dp"           android:layout_marginRight="11dp"           android:text="编辑" />   </RelativeLayout>    <ExpandableListView        android:id="@+id/elv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:layout_alignParentBottom="true"        android:background="@android:color/white"        android:gravity="center_vertical">        <CheckBox            android:id="@+id/checkbox2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_marginLeft="10dp"            android:focusable="false" />        <TextView            android:layout_width="wrap_content"            android:layout_height="50dp"            android:layout_centerVertical="true"            android:layout_marginLeft="10dp"            android:layout_toRightOf="@+id/checkbox2"            android:gravity="center_vertical"            android:text="全选"            android:textSize="20sp" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:orientation="horizontal">       <LinearLayout           android:visibility="visible"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:orientation="horizontal">           <TextView               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_marginLeft="10dp"               android:text="合计 :" />           <TextView               android:id="@+id/tv_price"               android:layout_width="wrap_content"               android:layout_height="50dp"               android:layout_marginLeft="10dp"               android:paddingRight="10dp"               android:text="0"               android:textColor="@android:color/holo_red_light" />           <TextView               android:id="@+id/tv_num"               android:layout_width="wrap_content"               android:layout_height="50dp"               android:background="@android:color/holo_red_dark"               android:gravity="center"               android:padding="10dp"               android:text="结算(0)"               android:textColor="@android:color/white" />       </LinearLayout>          <LinearLayout              android:visibility="gone"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:orientation="horizontal">              <Button                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="移至收藏夹"/>              <Button                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:text="删除"                  android:id="@+id/del"/>          </LinearLayout>del        </LinearLayout>    </RelativeLayout></LinearLayout>
 布局文件(child)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:background="@android:color/darker_gray"    android:gravity="center_vertical"    android:orientation="horizontal">    <CheckBox        android:id="@+id/cb_child"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="30dp"        android:layout_marginLeft="40dp"        android:layout_marginTop="30dp"        android:focusable="false" />    <LinearLayout        android:layout_width="100dp"        android:layout_height="120dp"        android:orientation="vertical">        <com.facebook.drawee.view.SimpleDraweeView            android:layout_width="100dp"            android:layout_height="120dp"            android:background="@mipmap/ic_launcher"            android:id="@+id/fresco"/>    </LinearLayout><!--这是包括两个视图-->    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"><!--商品详情的视图-->        <LinearLayout            android:id="@+id/ll_price"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:orientation="vertical"            android:visibility="visible">            <TextView                android:id="@+id/tv_title"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="¥3000.00,我就是三千块钱呀" />            <TextView                android:id="@+id/tv_time"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="2017年" />            <TextView                android:id="@+id/iv_price"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="3000" />            <TextView                android:id="@+id/tv_num"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="数量" />        </LinearLayout><!--编辑的视图-->        <LinearLayout            android:id="@+id/ll_num"            android:visibility="gone"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal">            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:orientation="horizontal">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="+"                    android:id="@+id/add"/>                <TextView                    android:id="@+id/num1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="数量" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="-"                    android:id="@+id/plus"/>                <Button                    android:layout_width="40dp"                    android:layout_height="wrap_content"                    android:text="删除"                    android:id="@+id/del"/>            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>
parent文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="100dp"    android:gravity="center_vertical"    android:orientation="horizontal">    <CheckBox        android:id="@+id/cb_parent"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="30dp"        android:layout_marginLeft="20dp"        android:layout_marginTop="30dp"        android:focusable="false" />    <TextView        android:id="@+id/tv_sign"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:text="商家" />    <TextView        android:id="@+id/tv_number"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="200dp"        android:text="编辑" /></LinearLayout>
适配器吧

package com.example.shoppingcart.adapter;import android.content.Context;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.example.shoppingcart.R;import com.example.shoppingcart.bean.DelBean;import com.example.shoppingcart.bean.GoodsBean;import com.example.shoppingcart.eventbus.MessageEvent;import com.example.shoppingcart.eventbus.PriceAndCountEvent;import com.example.shoppingcart.net.RetrofitHelper;import com.facebook.drawee.view.SimpleDraweeView;import org.greenrobot.eventbus.EventBus;import java.util.List;import rx.Observer;import rx.Subscriber;import rx.android.schedulers.AndroidSchedulers;import rx.schedulers.Schedulers;public class MyElvAdapter extends BaseExpandableListAdapter {    private Context context;    private List<GoodsBean.DataBean> groupList;    private List<List<GoodsBean.DataBean.ListBean>> childList;    private LayoutInflater inflater;    //未点击的状态    private Boolean i01 = false;    private AlertDialog show;    public MyElvAdapter(Context context, List<GoodsBean.DataBean> groupList, List<List<GoodsBean.DataBean.ListBean>> childList) {        this.context = context;        this.groupList = groupList;        this.childList = childList;        inflater = LayoutInflater.from(context);    }    @Override    public int getGroupCount() {        return groupList.size();    }    @Override    public int getChildrenCount(int i) {        return childList.get(i).size();    }    @Override    public Object getGroup(int i) {        return groupList.get(i);    }    @Override    public Object getChild(int i, int i1) {        return childList.get(i).get(i1);    }    @Override    public long getGroupId(int i) {        return i;    }    @Override    public long getChildId(int i, int i1) {        return i1;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {        final MyParentViewHolder myParentViewHolder;        if(view==null){            myParentViewHolder = new MyParentViewHolder();            view = inflater.inflate(R.layout.elv_parent,null);            myParentViewHolder.cb_parent = view.findViewById(R.id.cb_parent);            myParentViewHolder.tv_sign = view.findViewById(R.id.tv_sign);            myParentViewHolder.tv_num = view.findViewById(R.id.tv_number);            view.setTag(myParentViewHolder);        }else{            myParentViewHolder = (MyParentViewHolder) view.getTag();        }        final GoodsBean.DataBean dataBean = groupList.get(i);        myParentViewHolder.tv_sign.setText(dataBean.getSellerName());        myParentViewHolder.cb_parent.setChecked(dataBean.isChecked());        myParentViewHolder.tv_num.setText(dataBean.getBj());        //设置一级列表的点击事件        myParentViewHolder.cb_parent.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                dataBean.setChecked(myParentViewHolder.cb_parent.isChecked());                //改变二级列表的选中状态                ChangeChildState(i,myParentViewHolder.cb_parent.isChecked());                EventBus.getDefault().post(compute());                //改变全选按钮的状态                changeAllCbState(isAllGroupChecked());                //刷新                notifyDataSetChanged();            }        });        //点击编辑按钮        myParentViewHolder.tv_num.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //拿到一级列表下的二级列表                List<GoodsBean.DataBean.ListBean> list = dataBean.getList();                if(i01==false){                    i01=true;                    dataBean.setBj("编辑");                    //给一级下的二级列表设置为false                    for (int i = 0; i < list.size(); i++) {                        list.get(i).setShow(false);                    }                    notifyDataSetChanged();                }else{                    i01 = false;                    dataBean.setBj("完成");                    for (int i = 0; i < list.size(); i++) {                        list.get(i).setShow(true);                    }                    notifyDataSetChanged();                }            }        });        myParentViewHolder.cb_parent.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                dataBean.setChecked(myParentViewHolder.cb_parent.isChecked());                ChangeChildState(i, myParentViewHolder.cb_parent.isChecked());                EventBus.getDefault().post(compute());                changeAllCbState(isAllGroupChecked());                notifyDataSetChanged();            }        });        return view;    }    @Override    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {       final MyChildViewHolder myChildViewHolder;       if(view==null){           myChildViewHolder = new MyChildViewHolder();           view = inflater.inflate(R.layout.elv_child,null);           myChildViewHolder.cb_child = view.findViewById(R.id.cb_child);           myChildViewHolder.frescon = view.findViewById(R.id.fresco);           myChildViewHolder.price = view.findViewById(R.id.iv_price);           myChildViewHolder.num = view.findViewById(R.id.tv_num);           myChildViewHolder.title = view.findViewById(R.id.tv_title);           myChildViewHolder.time = view.findViewById(R.id.tv_time);           myChildViewHolder.num1= view.findViewById(R.id.num1);           myChildViewHolder.del = view.findViewById(R.id.del);           myChildViewHolder.add = view.findViewById(R.id.add);           myChildViewHolder.plus = view.findViewById(R.id.plus);           myChildViewHolder.ll_num = view.findViewById(R.id.ll_num);           myChildViewHolder.ll_price = view.findViewById(R.id.ll_price);           view.setTag(myChildViewHolder);       }else{           myChildViewHolder = (MyChildViewHolder) view.getTag();       }        final GoodsBean.DataBean.ListBean listBean = childList.get(i).get(i1);        myChildViewHolder.cb_child.setChecked(listBean.isChecked());        myChildViewHolder.title.setText(listBean.getTitle());        myChildViewHolder.time.setText(listBean.getCreatetime());        myChildViewHolder.price.setText(listBean.getPrice()+"");        myChildViewHolder.num.setText(listBean.getNum()+"");        myChildViewHolder.num1.setText(listBean.getNum()+"");        //二级checkbox        myChildViewHolder.cb_child.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //设置该条目对象里的checked属性值                listBean.setChecked(myChildViewHolder.cb_child.isChecked());                PriceAndCountEvent priceAndCountEvent = compute();                EventBus.getDefault().post(priceAndCountEvent);                if (myChildViewHolder.cb_child.isChecked()) {                    //当前checkbox是选中状态                    if (isAllChildChecked(i)) {                        ChangeParentState(i, true);                        changeAllCbState(isAllGroupChecked());                    }                } else {                    ChangeParentState(i, false);                    changeAllCbState(isAllGroupChecked());                }                notifyDataSetChanged();            }        });        if(listBean.getShow()==false){            myChildViewHolder.ll_price.setVisibility(View.VISIBLE);            myChildViewHolder.ll_num.setVisibility(View.GONE);        } else {            myChildViewHolder.ll_price.setVisibility(View.GONE);            myChildViewHolder.ll_num.setVisibility(View.VISIBLE);        }        //加号点击事件        myChildViewHolder.add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                int num1 = listBean.getNum();                num1++;                myChildViewHolder.num1.setText(num1+"");                myChildViewHolder.num.setText(num1+"");                listBean.setNum(num1);                if(myChildViewHolder.cb_child.isChecked()){                    //将价钱进行传递                    PriceAndCountEvent priceAndCountEvent = compute();                    EventBus.getDefault().post(priceAndCountEvent);                }            }        });        //减号        myChildViewHolder.plus.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                int num1 = listBean.getNum();                if(num1==1){                    Toast.makeText(context,"数量不能小于1",Toast.LENGTH_SHORT).show();;                    return;                }                myChildViewHolder.num1.setText(--num1+"");                myChildViewHolder.num.setText(num1+"");                listBean.setNum(num1);                if(myChildViewHolder.cb_child.isChecked()){                    //将价钱进行传递                    PriceAndCountEvent priceAndCountEvent = compute();                    EventBus.getDefault().post(priceAndCountEvent);                }            }        });        final String pid = listBean.getPid()+"";        //删除        myChildViewHolder.del.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                final AlertDialog.Builder normalDialog =                        new AlertDialog.Builder(context);                //normalDialog.setIcon(R.drawable.selectheart);                normalDialog.setTitle("确认要删除此商品吗");                normalDialog.setPositiveButton("确定",                        new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialogInterface, final int b ) {                                                            RetrofitHelper.serviceApi().delList(pid)                                                                    .subscribeOn(Schedulers.io())                                                                    .observeOn(AndroidSchedulers.mainThread())                                                                    .subscribe(new Subscriber<DelBean>() {                                                                        @Override                                                                        public void onCompleted() {                                                                        }                                                                        @Override                                                                        public void onError(Throwable e) {                                                                        }                                                                        @Override                                                                        public void onNext(DelBean delBean) {                                                                            if (delBean.getCode().equals("0")) {                                                                                List<GoodsBean.DataBean.ListBean> listBeans = childList.get(i);                                                                                listBeans.remove(i1);                                                                                Log.e("tag",listBeans.size()+"");                                                                                if (listBeans.size() == 0) {                                                                                    childList.remove(i);                                                                                    groupList.remove(i);                                                    }                                                    EventBus.getDefault().post(compute());                                                    notifyDataSetChanged();                                                } else {                                                    Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();                                                }                                            }                                        });                                //Log.e("tag",childList.size()+"");                                notifyDataSetChanged();                            }                        });                normalDialog.setNegativeButton("关闭",                        new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                //...To-do                                show.dismiss();                            }                        });                // 显示                show = normalDialog.show();            }        });        notifyDataSetChanged();        return view;    }    @Override    public boolean isChildSelectable(int i, int i1) {        return true;    }    class MyChildViewHolder{        CheckBox cb_child;        SimpleDraweeView frescon;        TextView title,price,time,num,num1,add,plus,del;        LinearLayout ll_price,ll_num;    }    class MyParentViewHolder{        CheckBox cb_parent;        TextView tv_sign;        TextView tv_num;    }    //改变二级列表的选中状态    public void ChangeChildState(int groupPosition,boolean flag){        List<GoodsBean.DataBean.ListBean> listBeans = childList.get(groupPosition);        for (int i = 0; i <listBeans.size() ; i++) {            GoodsBean.DataBean.ListBean listBean = listBeans.get(i);            listBean.setChecked(flag);        }    }    //改变一级列表的选中状态    public void ChangeParentState(int groupPosition,boolean flag){            GoodsBean.DataBean dataBean = groupList.get(groupPosition);            dataBean.setChecked(flag);    }    //判断一级列表是否全部选中    public boolean isAllGroupChecked(){        for (int i = 0; i <groupList.size() ; i++) {            GoodsBean.DataBean dataBean = groupList.get(i);            if(!dataBean.isChecked()){                return false;            }        }        return true;    }    //判断二级列表是否全部选中    public boolean isAllChildChecked(int groupPosition){        List<GoodsBean.DataBean.ListBean> listBeans = childList.get(groupPosition);        for (int i = 0; i <listBeans.size() ; i++) {            GoodsBean.DataBean.ListBean listBean = listBeans.get(i);            if(!listBean.isChecked()){                return false;            }        }        return true;    }    /**     * 计算列表中,选中的钱和数量     */    private PriceAndCountEvent compute() {        int count = 0;        int price = 0;        for (int i = 0; i < childList.size(); i++) {            List<GoodsBean.DataBean.ListBean> listBeans = childList.get(i);            for (int j = 0; j < listBeans.size(); j++) {                GoodsBean.DataBean.ListBean listBean = listBeans.get(j);                if (listBean.isChecked()) {                    price += listBean.getNum() * listBean.getPrice();                    count += listBean.getNum();                }            }        }        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();        priceAndCountEvent.setCount(count);        priceAndCountEvent.setPrice(price);        return priceAndCountEvent;    }    /**     * 如果全选按钮点击,改变一级和二级列表的状态     *     * @param flag     */    public void changeAllListCbState(boolean flag) {        for (int i = 0; i < groupList.size(); i++) {            ChangeChildState(i, flag);            ChangeParentState(i, flag);        }        EventBus.getDefault().post(compute());        notifyDataSetChanged();    }    /**     * 改变全选按钮的状态     *     * @param flag     */    private void changeAllCbState(boolean flag) {        MessageEvent messageEvent = new MessageEvent();        messageEvent.setChecked(flag);        EventBus.getDefault().post(messageEvent);    }}
bean 中

一级列表的bean

private String bj = "编辑01";private boolean checked;public String getBj() {    return bj;}public void setBj(String bj) {    this.bj = bj;}public boolean isChecked() {    return checked;}public void setChecked(boolean checked) {    this.checked = checked;}

child中的bean

private Boolean isShow = true;private boolean checked;public Boolean getShow() {    return isShow;}public void setShow(Boolean show) {    isShow = show;}public boolean isChecked() {    return checked;}public void setChecked(boolean checked) {    this.checked = checked;}