Android实现购物车逻辑
来源:互联网 发布:qq企业邮箱 域名 编辑:程序博客网 时间:2024/06/09 01:16
//1.先创建eventbusevent包;2.创建MessageEvent类;3.创建PriceAndCountEvent类,用来存储价格和数量
public class MessageEvent { private boolean checked; public boolean isChecked(){ return checked; }; public void setChecked(boolean checked){ this.checked = checked; }}
public class PriceAndCountEvent { private int price; private int count; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; }}//购物车页面
public class Fragment4 extends Fragment implements IFragment4 { private View view; private ExpandableListView mElv; private CheckBox mCb1; private TextView mPrice; Fragment4Adapter adapter; /** * 去结算 */ private TextView mJiesuan; int count; int price; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment4, null); initView(view); new Fragment4Presenter(this).getGoods(); EventBus.getDefault().register(this); return view; } private void initView(View view) { mElv = (ExpandableListView) view.findViewById(R.id.elv); mCb1 = (CheckBox) view.findViewById(R.id.cb1); mPrice = (TextView) view.findViewById(R.id.price); mJiesuan = (TextView) view.findViewById(R.id.jiesuan); mCb1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.changeAllListCbState(mCb1.isChecked()); } }); } @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Override public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) { adapter = new Fragment4Adapter(getActivity(), groupList, childList); mElv.setAdapter(adapter); mElv.setGroupIndicator(null); //默认让其全部展开 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } } @Subscribe public void onMessageEvent(MessageEvent event){ mCb1.setChecked(event.isChecked()); } @Subscribe public void onMessageEvent(PriceAndCountEvent event) { mJiesuan.setText("结算(" + event.getCount() + ")"); mPrice.setText(event.getPrice() + ""); }}//二级列表适配器
public class Fragment4Adapter extends BaseExpandableListAdapter { private Context context; private List<GoosBean.DataBean> grouplist; private List<List<GoosBean.DataBean.DatasBean>> childlist; private LayoutInflater inflater; private IFragment4 iFragment4; public Fragment4Adapter(Context context, List<GoosBean.DataBean> grouplist, List<List<GoosBean.DataBean.DatasBean>> 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 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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder holder; if (convertView == null){ holder = new GroupViewHolder(); convertView = inflater.inflate(R.layout.item_parent,null); holder.cbGroup = (CheckBox) convertView.findViewById(R.id.cb_parent); holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number); convertView.setTag(holder); } else { holder = (GroupViewHolder) convertView.getTag(); } final GoosBean.DataBean dataBean = grouplist.get(groupPosition); holder.cbGroup.setChecked(dataBean.isChecked()); holder.tv_number.setText(dataBean.getTitle()); holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dataBean.setChecked(holder.cbGroup.isChecked()); changeChildCbState(groupPosition,holder.cbGroup.isChecked()); EventBus.getDefault().post(computer()); changeAllCbState(isAllGroupCbSelected()); notifyDataSetChanged(); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder holder; if (convertView == null){ holder = new ChildViewHolder(); convertView = inflater.inflate(R.layout.item_child,null); holder.cbChild = (CheckBox) convertView.findViewById(R.id.cb_child); holder.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel); holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content); holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time); holder.tv_pri = (TextView) convertView.findViewById(R.id.tv_pri); holder.jian = (TextView) convertView.findViewById(R.id.jian); holder.num = (TextView) convertView.findViewById(R.id.num); holder.jia = (TextView) convertView.findViewById(R.id.jia); holder.del = (TextView) convertView.findViewById(R.id.del); convertView.setTag(holder); } else { holder = (ChildViewHolder) convertView.getTag(); } final GoosBean.DataBean.DatasBean datasBean = childlist.get(groupPosition).get(childPosition); holder.cbChild.setChecked(datasBean.isChecked()); holder.tv_tel.setText(datasBean.getType_name()); holder.tv_content.setText(datasBean.getMsg()); holder.tv_time.setText(datasBean.getAdd_time()); holder.tv_pri.setText(datasBean.getPrice()+""); //给二级CheckBox设置点击事件 holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置该条目对象里面的CheckBox属性值 datasBean.setChecked(holder.cbChild.isChecked()); EventBus.getDefault().post(computer()); if (holder.cbChild.isChecked()){ //当前CheckBox是选中状态 if (isAllChildCbSelected(groupPosition)){ changeGroupCbState(groupPosition,true); changeAllCbState(isAllGroupCbSelected()); } } else { //取消选中 changeGroupCbState(groupPosition,false); changeAllCbState(isAllGroupCbSelected()); } notifyDataSetChanged(); } }); //加号-数量加 holder.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = datasBean.getNum(); holder.num.setText(++num+""); datasBean.setNum(num); if (holder.cbChild.isChecked()){ EventBus.getDefault().post(computer()); } } }); //减号-数量减 holder.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = datasBean.getNum(); if (num==1){ return; } holder.num.setText(--num+""); datasBean.setNum(num); if (holder.cbChild.isChecked()){ EventBus.getDefault().post(computer()); } } }); //删除商品 holder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<GoosBean.DataBean.DatasBean> datasBeen = childlist.get(groupPosition); GoosBean.DataBean.DatasBean remove = datasBeen.remove(childPosition); if (datasBeen.size() == 0){ childlist.remove(groupPosition); grouplist.remove(groupPosition); } EventBus.getDefault().post(computer()); notifyDataSetChanged(); } }); return convertView; } class GroupViewHolder{ CheckBox cbGroup; TextView tv_number; } class ChildViewHolder { CheckBox cbChild; TextView tv_tel; TextView tv_content; TextView tv_time; TextView tv_pri; TextView jian; TextView num; TextView jia; TextView del; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //改变全选CheckBox状态 private void changeAllCbState(boolean flag){ MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); } //改变二级列表CheckBox状态 private void changeChildCbState(int groupPosition,boolean flag){ List<GoosBean.DataBean.DatasBean> datasBeen = childlist.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i); datasBean.setChecked(flag); } } //改变一级列表CheckBox状态 private void changeGroupCbState(int groupPosition,boolean flag){ GoosBean.DataBean dataBean = grouplist.get(groupPosition); dataBean.setChecked(flag); } //判断一级列表是否全部选中 public boolean isAllGroupCbSelected(){ for (int i = 0; i < grouplist.size(); i++) { GoosBean.DataBean dataBean = grouplist.get(i); if (!dataBean.isChecked()){ return false; } } return true; } //判断二级列表是否全部选中 public boolean isAllChildCbSelected(int groupPosition){ List<GoosBean.DataBean.DatasBean> datasBeen = childlist.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i); if (!datasBean.isChecked()){ return false; } } return true; } //计算列表中选中的钱和数量 private PriceAndCountEvent computer(){ int count = 0; int price = 0; for (int i = 0; i < childlist.size(); i++) { List<GoosBean.DataBean.DatasBean> datasBeen = childlist.get(i); for (int j = 0; j < datasBeen.size(); j++) { GoosBean.DataBean.DatasBean datasBean = datasBeen.get(j); if (datasBean.isChecked()){ count+=datasBean.getNum(); price+=datasBean.getNum()*datasBean.getPrice(); } } } PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setCount(count); priceAndCountEvent.setPrice(price); return priceAndCountEvent; } //设置全选、反选 public void changeAllListCbState(boolean flag){ for (int i = 0; i < grouplist.size(); i++) { changeGroupCbState(i,flag); changeChildCbState(i,flag); } EventBus.getDefault().post(computer()); notifyDataSetChanged(); }}//数据接口public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
阅读全文
0 0
- Android实现购物车逻辑
- Android 购物车页面和逻辑实现
- Android 购物车UI及逻辑实现
- Android 购物车页面和逻辑实现
- 购物车实现逻辑
- 购物车实现逻辑
- Android商城购物车页面实现和逻辑实现
- 购物车实现逻辑【详解】
- 购物车实现逻辑【详解】
- 购物车逻辑实现 思路
- Android 双RecyclerView嵌套实现购物车逻辑
- 简单购物车逻辑实现代码
- 购物车的整体实现逻辑
- Android-------------实现购物车
- Android实现购物车
- TP 购物车逻辑
- 购物车 全选逻辑
- 购物车逻辑
- java 笔记 split 分割字符串
- opengl 坐标的理解
- centos 安装scp
- Linux cd命令
- Tensorflow一些常用基本概念与函数
- Android实现购物车逻辑
- 0—999的水仙花数
- hiho 1638 小Hi的天平 [Offer收割]编程练习赛36 Problem B 并查集+图dfs乱搞
- dubbo处理自定义异常问题
- 多态
- linux 代码实现文件夹及其文件的
- 郝斌老师C语言学习笔记----基本类型指针
- SpringBoot--整合RabbitMq
- [bzoj2761][暴力]不重复数字