展示效果

Android 购物车(精仿)可删可全选与反选_第1张图片

主Activity中的方法

public class MainActivity extends Activity implements ShopcartExpandableListViewAdapter.CheckInterface, ShopcartExpandableListViewAdapter.ModifyCountInterface, OnClickListener{    private ExpandableListView exListView;    private CheckBox cb_check_all;    private TextView tv_total_price;    private TextView tv_delete;    private TextView tv_go_to_pay;    private Context context;    private double totalPrice = 0.00;// 购买的商品总价    private int totalCount = 0;// 购买的商品总数量    private ShopcartExpandableListViewAdapter selva;    private List groups = new ArrayList();// 组元素数据列表    private Map> children = new HashMap>();// 子元素数据列表    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        initView();        initEvents();    }    private void initView()    {        context = this;        virtualData();        exListView = (ExpandableListView) findViewById(R.id.exListView);        cb_check_all = (CheckBox) findViewById(R.id.all_chekbox);        tv_total_price = (TextView) findViewById(R.id.tv_total_price);        tv_delete = (TextView) findViewById(R.id.tv_delete);        tv_go_to_pay = (TextView) findViewById(R.id.tv_go_to_pay);    }    private void initEvents()    {        selva = new ShopcartExpandableListViewAdapter(groups, children, this);        selva.setCheckInterface(this);// 关键步骤1,设置复选框接口        selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口        exListView.setAdapter(selva);        for (int i = 0; i < selva.getGroupCount(); i++)        {            exListView.expandGroup(i);// 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现        }        cb_check_all.setOnClickListener(this);        tv_delete.setOnClickListener(this);        tv_go_to_pay.setOnClickListener(this);    }    /**     * 模拟数据
* 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,
* 其键是组元素的Id(通常是一个唯一指定组元素身份的值) */
private void virtualData() { for (int i = 0; i < 6; i++) { groups.add(new GroupInfo(i + "", "第八号当铺" + (i + 1) + "号店")); List products = new ArrayList(); for (int j = 0; j <= i; j++) { products.add(new ProductInfo(j + "", "商品", "", groups.get(i).getName() + "的第" + (j + 1) + "个商品", 120.00 + i * j, 1)); } children.put(groups.get(i).getId(), products);// 将组元素的一个唯一值,这里取Id,作为子元素List的Key } } @Override public void onClick(View v) { AlertDialog alert; switch (v.getId()) { case R.id.all_chekbox: doCheckAll(); break; case R.id.tv_go_to_pay: if (totalCount == 0) { Toast.makeText(context, "请选择要支付的商品", Toast.LENGTH_LONG).show(); return; } alert = new AlertDialog.Builder(context).create(); alert.setTitle("操作提示"); alert.setMessage("总计:\n" + totalCount + "种商品\n" + totalPrice + "元"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.show(); break; case R.id.tv_delete: if (totalCount == 0) { Toast.makeText(context, "请选择要移除的商品", Toast.LENGTH_LONG).show(); return; } alert = new AlertDialog.Builder(context).create(); alert.setTitle("操作提示"); alert.setMessage("您确定要将这些商品从购物车中移除吗?"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { doDelete(); } }); alert.show(); break; } } /** * 删除操作
* 1.不要边遍历边删除,容易出现数组越界的情况
* 2.现将要删除的对象放进相应的列表容器中,待遍历完后,以removeAll的方式进行删除 */
protected void doDelete() { List toBeDeleteGroups = new ArrayList();// 待删除的组元素列表 for (int i = 0; i < groups.size(); i++) { GroupInfo group = groups.get(i); if (group.isChoosed()) { toBeDeleteGroups.add(group); } List toBeDeleteProducts = new ArrayList();// 待删除的子元素列表 List childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j++) { if (childs.get(j).isChoosed()) { toBeDeleteProducts.add(childs.get(j)); } } childs.removeAll(toBeDeleteProducts); } groups.removeAll(toBeDeleteGroups); selva.notifyDataSetChanged(); calculate(); } @Override public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { ProductInfo product = (ProductInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); currentCount++; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount + ""); selva.notifyDataSetChanged(); calculate(); } @Override public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { ProductInfo product = (ProductInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); if (currentCount == 1) return; currentCount--; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount + ""); selva.notifyDataSetChanged(); calculate(); } @Override public void checkGroup(int groupPosition, boolean isChecked) { GroupInfo group = groups.get(groupPosition); List childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i++) { childs.get(i).setChoosed(isChecked); } if (isAllCheck()) cb_check_all.setChecked(true); else cb_check_all.setChecked(false); selva.notifyDataSetChanged(); calculate(); } @Override public void checkChild(int groupPosition, int childPosiTion, boolean isChecked) { boolean allChildSameState = true;// 判断改组下面的所有子元素是否是同一种状态 GroupInfo group = groups.get(groupPosition); List childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i++) { if (childs.get(i).isChoosed() != isChecked) { allChildSameState = false; break; } } if (allChildSameState) { group.setChoosed(isChecked);// 如果所有子元素状态相同,那么对应的组元素被设为这种统一状态 } else { group.setChoosed(false);// 否则,组元素一律设置为未选中状态 } if (isAllCheck()) cb_check_all.setChecked(true); else cb_check_all.setChecked(false); selva.notifyDataSetChanged(); calculate(); } private boolean isAllCheck() { for (GroupInfo group : groups) { if (!group.isChoosed()) return false; } return true; } /** 全选与反选 */ private void doCheckAll() { for (int i = 0; i < groups.size(); i++) { groups.get(i).setChoosed(cb_check_all.isChecked()); GroupInfo group = groups.get(i); List childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j++) { childs.get(j).setChoosed(cb_check_all.isChecked()); } } selva.notifyDataSetChanged(); } /** * 统计操作
* 1.先清空全局计数器
* 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作
* 3.给底部的textView进行数据填充 */
private void calculate() { totalCount = 0; totalPrice = 0.00; for (int i = 0; i < groups.size(); i++) { GroupInfo group = groups.get(i); List childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j++) { ProductInfo product = childs.get(j); if (product.isChoosed()) { totalCount++; totalPrice += product.getPrice() * product.getCount(); } } } tv_total_price.setText("¥" + totalPrice); tv_go_to_pay.setText("去支付(" + totalCount + ")"); }}

主要代码点击这里吧(7smu)

谢谢观看,小编祝大家生活愉快!(多多关注小编,会有非常之多精彩分享哦!)

更多相关文章

  1. Android 自定义分享列表ACTION_SEND
  2. Android | activity之间传递列表,以listview显示
  3. android最近任务列表,删除某个应用操作
  4. Android 的res/values/colors自定义颜色列表和注释表及布局文件
  5. 【Android】ExpandableListView二级列表使用介绍
  6. android2.3 api demo 学习系列(1)--apidemo主列表的实现
  7. Android 自学之列表选择框Spinner

随机推荐

  1. popupwindow的一些注意事项
  2. Android(安卓)特殊符号的转码大全
  3. Android(安卓)类代码防止反编译的办法
  4. AsyncTask简单入门
  5. android初学之Hello World
  6. Android(安卓)Permission完整版
  7. Android解决加载大图片时内存溢出的问题
  8. Android(安卓)四大组件之Acticity
  9. 百度网盘邀请码:还剩三个哦
  10. eclipse中OpenCV安装指南