完整版二级购物车
来源:互联网 发布:京东店铺优化 编辑:程序博客网 时间:2024/05/22 13:52
//自定义View
public class AddDelView extends LinearLayout { private TextView num; private OnItemClick onItemClick; private TextView add; private TextView del; public interface OnItemClick { public void onItemAddClick(int count); public void onItemDelClick(int count); } public void setOnItemClick(OnItemClick onItemClick) { this.onItemClick = onItemClick; } public AddDelView(Context context) { this(context, null); } public AddDelView(final Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.add_del_view, this); add = findViewById(R.id.add); del = findViewById(R.id.del); num = findViewById(R.id.num); } public void setOnAddClickListener(OnClickListener onClickListener) { add.setOnClickListener(onClickListener); } public void setOnDelClickListener(OnClickListener onClickListener) { del.setOnClickListener(onClickListener); } public void setCount(String count) { num.setText(count); } public String getCount(){ return num.getText().toString().trim(); }}
//childBean
public class ChildBean { private String childName; private boolean checked; private float money; private int count; public ChildBean(String childName, boolean checked, float money, int count) { this.childName = childName; this.checked = checked; this.money = money; this.count = count; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getChildName() { return childName; } public void setChildName(String childName) { this.childName = childName; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; }}
//GroupBean
public class GroupBean { private String groupName; private boolean checked; public GroupBean() { } public GroupBean(String groupName, boolean checked) { this.groupName = groupName; this.checked = checked; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; }}
//Main
public class MainActivity extends AppCompatActivity { private List<GroupBean> groupList = new ArrayList<>(); private List<List<ChildBean>> childList = new ArrayList<>(); private ExpandableListView mElv; /** * 全选 */ private CheckBox mCbAll; /** * 合计: */ private TextView mTvTotal; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); initData(); adapter = new MyAdapter(this,groupList,childList); mElv.setGroupIndicator(null); mElv.setAdapter(adapter); for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } mCbAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.allChecked(mCbAll.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void messageCountEvent(MessageCounEvent msg){ mTvTotal.setText("总计:"+msg.getCount()+"个 共"+msg.getMoney()+"元"); } @Subscribe public void messageEvent(MessageEvent msg){ mCbAll.setChecked(msg.isFlag()); } private void initData() { for (int i = 0; i < 3; i++) { GroupBean groupBean = new GroupBean("商家" + i, false); groupList.add(groupBean); List<ChildBean> list = new ArrayList<>(); for (int j = 0; j < 2; j++) { ChildBean childBean = new ChildBean("商品" + i, false, 12.0f, 1); list.add(childBean); } childList.add(list); } } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCbAll = (CheckBox) findViewById(R.id.cbAll); mTvTotal = (TextView) findViewById(R.id.tvTotal); }}
//二级列表适配器
public class MyAdapter extends BaseExpandableListAdapter { private Context context; private List<GroupBean> groupList = new ArrayList<>(); private List<List<ChildBean>> childList = new ArrayList<>(); 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) { View view; GroupViewHolder holder; if (convertView == null) { holder = new GroupViewHolder(); view = View.inflate(context, R.layout.item, null); holder.cb = view.findViewById(cb); holder.tv = view.findViewById(R.id.tvName); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } //赋值 GroupBean groupBean = groupList.get(groupPosition); holder.cb.setChecked(groupBean.isChecked()); holder.tv.setText(groupBean.getGroupName()); //给group的checkbox设置点击事件 holder.cb.setOnClickListener(new GroupCbOnClickListener(groupPosition)); return view; } @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view; final ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); view = View.inflate(context, R.layout.childitem, null); holder.cb = view.findViewById(cb); holder.tv = view.findViewById(R.id.tvName); holder.tvMoney = view.findViewById(R.id.tvPrice); holder.adv = view.findViewById(R.id.adv); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } //赋值 final ChildBean childBean = childList.get(groupPosition).get(childPosition); holder.cb.setChecked(childBean.isChecked()); holder.tv.setText(childBean.getChildName()); holder.tvMoney.setText(childBean.getMoney() + ""); holder.adv.setCount(childBean.getCount() + ""); holder.cb.setOnClickListener(new ChildCbOnClickListener(groupPosition, childPosition)); holder.adv.setOnAddClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String countStr = holder.adv.getCount(); int count = Integer.parseInt(countStr); holder.adv.setCount(++count + ""); childBean.setCount(count); //发送数量和价钱 childBean.setChecked(true); //判断该商家的所有商品的checkbox是否都选中 List<ChildBean> childBeanList = childList.get(groupPosition); if (isChildChecked(childBeanList)) { groupList.get(groupPosition).setChecked(true); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } else { groupList.get(groupPosition).setChecked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); } MessageCounEvent counEvent = new MessageCounEvent(); counEvent.setMoney(totalMoney()); counEvent.setCount(totalCount()); EventBus.getDefault().post(counEvent); } }); holder.adv.setOnDelClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String countStr = holder.adv.getCount(); int count = Integer.parseInt(countStr); count = --count < 1 ? 1 : count; holder.adv.setCount(count + ""); childBean.setCount(count); if (count > 1) { childBean.setChecked(true); } List<ChildBean> childBeanList = childList.get(groupPosition); if(isChildChecked(childBeanList)){ groupList.get(groupPosition).setChecked(true); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); }else{ groupList.get(groupPosition).setChecked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); } MessageCounEvent counEvent = new MessageCounEvent(); counEvent.setMoney(totalMoney()); counEvent.setCount(totalCount()); EventBus.getDefault().post(counEvent); notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox cb; TextView tv; } class ChildViewHolder { CheckBox cb; TextView tv; TextView tvMoney; AddDelView adv; } /** * 判断商家是否有未选中 */ private boolean isGroupChecked() { for (GroupBean groupBean : groupList) { if (!groupBean.isChecked()) { return false; } } return true; } /** * 二级Checkbox点击事件 */ class ChildCbOnClickListener implements View.OnClickListener { private int groupPosition; private int childPosition; public ChildCbOnClickListener(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { if (v instanceof CheckBox) { CheckBox cb = (CheckBox) v; List<ChildBean> childBeen = childList.get(groupPosition); ChildBean childBean = childBeen.get(childPosition); childBean.setChecked(cb.isChecked()); //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent counEvent = new MessageCounEvent(); counEvent.setCount(totalCount()); counEvent.setMoney(totalMoney()); EventBus.getDefault().post(counEvent); //判断该商家的所有商品的checkbox是否都选中 if (isChildChecked(childBeen)) { groupList.get(groupPosition).setChecked(true); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } else { groupList.get(groupPosition).setChecked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); } } } } /** * 一级Checkbox点击事件 */ class GroupCbOnClickListener implements View.OnClickListener { private int groupPostion; public GroupCbOnClickListener(int groupPostion) { this.groupPostion = groupPostion; } @Override public void onClick(View v) { if (v instanceof CheckBox) { CheckBox cb = (CheckBox) v; groupList.get(groupPostion).setChecked(cb.isChecked()); List<ChildBean> childBeen = childList.get(groupPostion); for (ChildBean childBean : childBeen) { childBean.setChecked(cb.isChecked()); } //计算商品数,发送主页面显示 MessageCounEvent counEvent = new MessageCounEvent(); counEvent.setMoney(totalMoney()); counEvent.setCount(totalCount()); EventBus.getDefault().post(counEvent); //判断其他的商家是否选中 MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } } } /** * 判断商品是否有未选中 */ private boolean isChildChecked(List<ChildBean> list) { for (ChildBean childBean : list) { if (!childBean.isChecked()) { return false; } } return true; } /** * 全选 */ public void allChecked(boolean bool) { for (int i = 0; i < groupList.size(); i++) { groupList.get(i).setChecked(bool); for (int j = 0; j < childList.get(i).size(); j++) { childList.get(i).get(j).setChecked(bool); } } //计算商品数 MessageCounEvent counEvent = new MessageCounEvent(); counEvent.setCount(totalCount()); counEvent.setMoney(totalMoney()); EventBus.getDefault().post(counEvent); notifyDataSetChanged(); } private int totalCount() { int count = 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).isChecked()) { int c = childList.get(i).get(j).getCount(); count += c; } } } return count; } private float totalMoney() { float money = 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).isChecked()) { //遍历所有的商品,只要是选中状态的,就计算价格 int c = childList.get(i).get(j).getCount(); float m = childList.get(i).get(j).getMoney(); money += c * m; } } } return money; }}
//countEvent
public class MessageCounEvent { private int count; private float money; public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } public int getCount() { return count; } public void setCount(int count) { this.count = count; }}
//msgEvent
public class MessageEvent { private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; }}
//自定义 + - 符号
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/circle_shape" android:gravity="center" android:paddingBottom="2dp" android:text="-" /> <TextView android:id="@+id/num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:gravity="center" android:text="1" /> <TextView android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/circle_shape" android:gravity="center" android:text="+" /></LinearLayout>
//父类布局
<?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="40dp" android:background="#330000ff" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" /></LinearLayout>
//子类布局
<?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="40dp" android:background="#330000ff" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" /> <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" /> <com.example.administrator.lasttext.AddDelView android:id="@+id/adv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp"></com.example.administrator.lasttext.AddDelView></LinearLayout>
//购物车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" tools:context="com.example.administrator.lasttext.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ff3660" android:gravity="center" android:text="购物车" android:textColor="#ffffff" android:textSize="20sp" /> <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:background="#33000000"> <CheckBox android:id="@+id/cbAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="合计:" /> </RelativeLayout></LinearLayout>
//自定义外圈
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="#33000000"></stroke> <solid android:color="@android:color/transparent"></solid></shape>
//EventBus依赖
compile 'org.greenrobot:eventbus:3.0.0'
阅读全文
0 0
- 二级购物车完整版
- 完整版二级购物车
- 二级列表购物车okhttp(完整版)
- 二级列表购物车
- 购物车二级列表
- 购物车二级
- 购物车(二级列表)
- 二级购物车
- 二级购物车
- 二级列表购物车
- 购物车二级列表
- 二级购物车ShoppingCart
- 二级购物车
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 二级购物车
- 购物车二级列表
- Spring Cloud分布式微服务云架构--简介
- SQL Server 2008通过LinkServer连接MySQL
- JavaScript之逻辑与、或、非运算
- Python常用模块使用笔记
- 树莓派Linux下访问Windows磁盘下的文件
- 完整版二级购物车
- [VS]VS2015 安装ReportDesigner/ReportViewer的方法
- 剑21-包含min函数的栈
- 第一章
- Windows 10操作系统上安装Linux Centos 7.2
- opencv学习——滑动条做调色板
- 欢迎使用CSDN-markdown编辑器
- js异步加载详解
- iOS怎样获取设备唯一标识符