二级购物车

来源:互联网 发布:山东省网络重点实验室 编辑:程序博客网 时间:2024/05/22 18:22
import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.TextView;import java.util.List;import de.greenrobot.event.EventBus;import test.bwie.com.goshopping.widget.MyAddDelView;/** * Created by dy on 2017/10/24. */public class MyAdapter extends BaseExpandableListAdapter {    private Context context;    private List<GroupBean> grouplist;    private List<List<ChildBean>> childlist;    private int num;    public MyAdapter(Context context, List<GroupBean> grouplist, List<List<ChildBean>> childlist) {        this.context = context;        this.grouplist = grouplist;        this.childlist = childlist;    }    @Override    public int getGroupCount() {        return grouplist.size();    }    @Override    public int getChildrenCount(int groupPosition) {        return childlist.get(groupPosition).size();    }    @Override    public Object getGroup(int groupPosition) {        return grouplist.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return childlist.get(groupPosition).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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        GroupViewHolder holder;        View view;        if (convertView == null) {            holder = new GroupViewHolder();            view = View.inflate(context, R.layout.head, null);            holder.head_check = view.findViewById(R.id.head_check);            holder.head_crad_name = view.findViewById(R.id.head_crad_name);            view.setTag(holder);        } else {            view = convertView;            holder = (GroupViewHolder) view.getTag();        }        GroupBean groupBean = grouplist.get(groupPosition);        holder.head_check.setChecked(groupBean.isBool());        holder.head_crad_name.setText(groupBean.getSup());        holder.head_check.setOnClickListener(new GroupCbOnClickListener(groupPosition));        return view;    }    @Override    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {        final ChildViewHolder holder;        View view;        if (convertView == null) {            holder = new ChildViewHolder();            view = View.inflate(context, R.layout.fragment3_card, null);            holder.fragment3_check = view.findViewById(R.id.fragment3_check);            holder.fragment3_crad_name = view.findViewById(R.id.fragment3_crad_name);            holder.fragment3_crad_price = view.findViewById(R.id.fragment3_crad_price);            holder.myadd = view.findViewById(R.id.myadd);            view.setTag(holder);        } else {            view = convertView;            holder = (ChildViewHolder) view.getTag();        }        final ChildBean childBean = childlist.get(groupPosition).get(childPosition);        holder.fragment3_check.setChecked(childBean.isChildcheck());        holder.fragment3_crad_name.setText(childBean.getGoosname());        holder.fragment3_crad_price.setText(childBean.getPrice() + "");        holder.fragment3_check.setOnClickListener(new ChildCbOnClickListener(groupPosition, childPosition, holder.myadd));        holder.myadd.setOnItemClick(new MyAddDelView.OnItemClick() {            private int count1;            @Override            public void onItemAddClick(int count) {                childlist.get(groupPosition).get(childPosition).setChildcheck(true);                List<ChildBean> childBeens = childlist.get(groupPosition);                if (isChildChecked(childBeens)) {                    grouplist.get(groupPosition).setBool(true);                    MessageEvent msg = new MessageEvent();                    msg.setFlag(isGroupChecked());                    EventBus.getDefault().post(msg);                    notifyDataSetChanged();                } else {                    grouplist.get(groupPosition).setBool(false);                    MessageEvent msg = new MessageEvent();                    msg.setFlag(false);                    EventBus.getDefault().post(msg);                    notifyDataSetChanged();                }                count1 = holder.myadd.getCount();                NumEvent numEvent = new NumEvent();                numEvent.setAllNum(totalCount());                numEvent.setNum(1);                EventBus.getDefault().post(numEvent);                childlist.get(groupPosition).get(childPosition).setCount(holder.myadd.getCount());                notifyDataSetChanged();                abc();            }            @Override            public void onItemDelClick(int count) {                int count2 = holder.myadd.getCount();                NumEvent numEvent = new NumEvent();                numEvent.setAllNum(totalCount());                numEvent.setNum(-1);                childlist.get(groupPosition).get(childPosition).setCount(holder.myadd.getCount());                EventBus.getDefault().post(numEvent);                abc();            }        });        return view;    }    //Child    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return false;    }    class GroupViewHolder {        CheckBox head_check;        TextView head_crad_name;    }    class ChildViewHolder {        CheckBox fragment3_check;        TextView fragment3_crad_name;        TextView fragment3_crad_price;        MyAddDelView myadd;    }    class GroupCbOnClickListener implements View.OnClickListener {        private int groupPostion;        public GroupCbOnClickListener(int groupPostion) {            this.groupPostion = groupPostion;        }        @Override        public void onClick(View v) {            int a = 0;            if (v instanceof CheckBox) {                CheckBox cb = (CheckBox) v;                //取消选中的时候,重置数据                List<ChildBean> childBeen = childlist.get(groupPostion);                    //查询该商家一共几件商品,然后传个负数到Activity                   /*  if (!cb.isChecked()) {                   for (ChildBean childBean : childBeen) {                        int count = childBean.getCount();                        a += count;                        *//*NumEvent num = new NumEvent();                        int b=count*childBean.getPrice();                        Log.i("TAG","group"+b+"ccccc");                        num.setMoney(-b);                        EventBus.getDefault().post(num);*//*                    }                    NumEvent numEvent = new NumEvent();                    numEvent.setNum(-a);                    EventBus.getDefault().post(numEvent);                    notifyDataSetChanged();                     }*/                /*else {                    for (ChildBean childBean : childBeen) {                        int count = childBean.getCount();                        int b=count*childBean.getPrice();                        Log.i("TAG","groupfalse"+b+"dddd");                        NumEvent numEvent = new NumEvent();                        numEvent.setMoney(b);                        EventBus.getDefault().post(numEvent);                    }                    notifyDataSetChanged();                }*/                if(!cb.isChecked()){                    abc();                    notifyDataSetChanged();                }                grouplist.get(groupPostion).setBool(cb.isChecked());                List<ChildBean> childBeanList = childlist.get(groupPostion);                for (ChildBean childBean : childBeanList) {                    childBean.setChildcheck(cb.isChecked());                }                MessageEvent event = new MessageEvent();                event.setFlag(isGroupChecked());                EventBus.getDefault().post(event);                NumEvent numEvent = new NumEvent();                numEvent.setAllNum(totalCount());                EventBus.getDefault().post(numEvent);                abc();                notifyDataSetChanged();            }        }    }    private boolean isGroupChecked() {        for (GroupBean groupBean : grouplist) {            if (!groupBean.isBool()) {                return false;            }        }        return true;    }    class ChildCbOnClickListener implements View.OnClickListener {        private int groupPosition;        private int childPosition;        private MyAddDelView myAddDelView;        public ChildCbOnClickListener(int groupPosition, int childPosition, MyAddDelView myadd) {            this.groupPosition = groupPosition;            this.childPosition = childPosition;            this.myAddDelView = myadd;        }        @Override        public void onClick(View v) {            if (v instanceof CheckBox) {                abc();                CheckBox cb = (CheckBox) v;                List<ChildBean> childBeens = childlist.get(groupPosition);                ChildBean childBean = childBeens.get(childPosition);                childBean.setChildcheck(cb.isChecked());                NumEvent numEvent = new NumEvent();                numEvent.setAllNum(totalCount());                EventBus.getDefault().post(numEvent);                if (isChildChecked(childBeens)) {                    grouplist.get(groupPosition).setBool(true);                    MessageEvent msg = new MessageEvent();                    msg.setFlag(isGroupChecked());                    EventBus.getDefault().post(msg);                    abc();                    notifyDataSetChanged();                } else {                    // Log.i("TAG","false1111111111");                    grouplist.get(groupPosition).setBool(false);                    MessageEvent msg = new MessageEvent();                    msg.setFlag(false);                    EventBus.getDefault().post(msg);                    abc();                    notifyDataSetChanged();                }                if (!cb.isChecked()) {                    int count = myAddDelView.getCount();                   /* NumEvent numEvent1 = new NumEvent();                    numEvent1.setNum(-count + 1);                    numEvent1.setAllNum(totalCount());                    EventBus.getDefault().post(numEvent1);*/                    abc();                    notifyDataSetChanged();                }            }        }    }    private boolean isChildChecked(List<ChildBean> childBeen) {        for (ChildBean bean : childBeen) {            if (!bean.isChildcheck()) {                return false;            }        }        return true;    }    public void allChecked(boolean bool) {        for (int i = 0; i < grouplist.size(); i++) {            grouplist.get(i).setBool(bool);            for (int j = 0; j < childlist.get(i).size(); j++) {                childlist.get(i).get(j).setChildcheck(bool);            }        }        NumEvent numEvent = new NumEvent();        numEvent.setAllNum(totalCount());        EventBus.getDefault().post(numEvent);        abc();        notifyDataSetChanged();    }    public int totalCount() {        num = 0;        for (int i = 0; i < grouplist.size(); i++) {            for (int j = 0; j < childlist.get(i).size(); j++) {                if (childlist.get(i).get(j).isChildcheck()) {                    //遍历所有的商品,只要是选中状态的,就加1                    num++;                }            }        }        return num;    }    public void abc() {        int b = 0;        int c=0;        int d=0;        for (int i = 0; i < grouplist.size(); i++) {            for (int j = 0; j < childlist.get(i).size(); j++) {                if (childlist.get(i).get(j).isChildcheck()) {                    int count = childlist.get(i).get(j).getCount();                    int price = childlist.get(i).get(j).getPrice();                    b = count * price;                    c+=b;                    d+=count;                }            }        }        NumEvent numEvent = new NumEvent();        numEvent.setMoney(c);        numEvent.setNum(d);        EventBus.getDefault().post(numEvent);        notifyDataSetChanged();    }}//mainActivity
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;import de.greenrobot.event.EventBus;import de.greenrobot.event.Subscribe;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private List<GroupBean> grouplist=new ArrayList<>();    private List<ChildBean> list=null;    private List<List<ChildBean>> childlist=new ArrayList<>();    private ExpandableListView mExpan;    private int sum=0;    private int doller=0;    private CheckBox mAllcheck;    /**     * 总计:     */    private TextView mTv;    /**     * 反选     */    private Button mFx;    /**     * 付款     */    private Button mBt;    private MyAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        EventBus.getDefault().register(this);        initView();        initDate();        adapter = new MyAdapter(MainActivity.this, grouplist, childlist);        mExpan.setAdapter(adapter);        mExpan.setGroupIndicator(null);        for (int i=0;i<grouplist.size();i++){            mExpan.expandGroup(i);        }    }    private void initView() {        mExpan = (ExpandableListView) findViewById(R.id.expan);        mAllcheck = (CheckBox) findViewById(R.id.allcheck);        mTv = (TextView) findViewById(R.id.tv);        mFx = (Button) findViewById(R.id.fx);        mFx.setOnClickListener(this);        mBt = (Button) findViewById(R.id.bt);        mBt.setOnClickListener(this);        mAllcheck.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                adapter.allChecked(mAllcheck.isChecked());            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.fx:                break;            case R.id.bt:                break;        }    }    public void initDate(){        for (int i=0;i<3;i++){            GroupBean groupBean = new GroupBean("商店" + i, false);            grouplist.add(groupBean);             list = new ArrayList<>();            for (int j=0;j<2;j++){                ChildBean childBean = new ChildBean("商品" + j, false, j + 1);                childBean.setCount(1);                list.add(childBean);            }            childlist.add(list);        }    }    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }    @Subscribe    public void messageEvent(MessageEvent msg){        mAllcheck.setChecked(msg.isFlag());    }    @Subscribe    public void NumEvent(NumEvent num){        /*int allNum = num.getAllNum();        Log.i("TAG","aaaaa"+allNum+"num");        int num1 = num.getNum();        Log.i("TAG","bbbbb"+num1+"num");        sum+=num1;        if(sum>=0&&allNum>=0){            mTv.setText("总计:"+(allNum+sum)+"件");        }else{            allNum=0;            sum=0;            mTv.setText("总计:0件");        }*/        int num1 = num.getNum();        Log.i("TAG",num1+"bbbb");        mTv.setText("总计:"+num1+"件");        int money = num.getMoney();        mBt.setText("总价:"+money+"元");    }}