尝试用ListView和ExpandableListView两种方式来实现安卓购物车。结果发现了一些很奇妙的问题。
当看见效果图上的东西时,很多人的第一印象是选择ListView来作为底层容器。用ListView的确可以实现。而且貌似扩展性比Expandable好。其ListView只有一个数据列表。该列表是一个由众多Map组成的List。无论是商品的还是商品所属的组,都是一个Map,不过在inflate视图的时候,会根据Map中的实现设置的flag进行选择性地加载相应的Item的layout。
关键处在于重写getItemViewType和getItemViewCount两个方法
package com.cjs.test.adapter;
import java.util.List;
import java.util.Map;
import com.cjs.test.entity.GroupInfo;
import com.cjs.test.entity.ProductInfo;
import com.cjs.test.test_mutilistview.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
/**
* ListView的购物车适配器
* @author chenjunsen
* 2015年8月18日下午3:56:22
*/
public class ShopCartListViewAdapter extends BaseAdapter{
private List
适配器的关键之处,在于通过暴露相关接口(checkInterface和modifyInterface),使得组选和子选的操作得以在主Activity中进行,避免了使用全局静态变量的可能。
package com.cjs.test.test_mutilistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cjs.test.adapter.ShopCartListViewAdapter;
import com.cjs.test.adapter.ShopCartListViewAdapter.CheckInterface;
import com.cjs.test.adapter.ShopCartListViewAdapter.ModifyCountInterface;
import com.cjs.test.entity.BaseInfo;
import com.cjs.test.entity.GroupInfo;
import com.cjs.test.entity.ProductInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* 用ListView实现购物车
* @author chenjunsen
* 2015年8月18日下午3:53:42
*/
public class ShopCartActivity extends Activity implements ModifyCountInterface, CheckInterface, OnClickListener {
private ListView listView;
private ShopCartListViewAdapter sclva;
private List> list=new ArrayList<>();//源数据列表
private List groups = new ArrayList<>();//组元素列表,对源数据列表进行二次选择封装,便于多选框选择变化时的操作
private Map> children = new HashMap<>();//子元素列表,对源数据列表进行二次选择封装,便于多选框选择变化时的操作
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;//购买的商品种类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_shop_cart);
initView();
initEvents();
}
private void initView() {
context = this;
listView = (ListView) findViewById(R.id.listView);
cb_check_all = (CheckBox) findViewById(R.id.cb_check_all);
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() {
virtualData();
sclva = new ShopCartListViewAdapter(list, this);
sclva.setCheckInterface(this);//设置多选框接口
sclva.setModifyCountInterface(this);//设置改变数量接口
listView.setAdapter(sclva);
cb_check_all.setOnClickListener(this);
tv_delete.setOnClickListener(this);
tv_go_to_pay.setOnClickListener(this);
}
/** 模拟数据*/
private void virtualData() {
String[] groupNames = new String[] { "涛博运动", "巅峰运动专卖店", "阿迪达斯专卖店", "潮流前线", "鞋王精品店", "扬帆精品" };
String[] groupIds = new String[] { "shop1", "shop2", "shop3", "shop4", "shop5", "shop6" };
for (int i = 0; i < groupNames.length; i++) {
Map group = new HashMap<>();
GroupInfo ginfo = new GroupInfo();
ginfo.setName(groupNames[i]);
ginfo.setId(groupIds[i]);
group.put(ShopCartListViewAdapter.SHOPCART_TYPE, ShopCartListViewAdapter.SHOPCART_FLAG_GROUP);
group.put(ShopCartListViewAdapter.SHOPCART_DATA, ginfo);
list.add(group);
groups.add(ginfo);
List childs = new ArrayList<>();
for (int j = 0; j <= i; j++) {
Map product = new HashMap<>();
ProductInfo pinfo = new ProductInfo();
pinfo.setName("运动鞋" + (j + 1));
pinfo.setDesc(groupNames[i] + "的鞋就是好,夏季促销,快来抢购!");
pinfo.setPrice(199 + 50 * j);
pinfo.setCount(1);
product.put(ShopCartListViewAdapter.SHOPCART_PARENT_ID, groupIds[i]);
product.put(ShopCartListViewAdapter.SHOPCART_PARENT_POSITION, i);
product.put(ShopCartListViewAdapter.SHOPCART_TYPE, ShopCartListViewAdapter.SHOPCART_FLAG_CHILDREN);
product.put(ShopCartListViewAdapter.SHOPCART_DATA, pinfo);
list.add(product);
childs.add(pinfo);
}
children.put(groupIds[i], childs);
}
}
@Override
public void checkGroup(int position, boolean isChecked) {
Map parent = list.get(position);
String parentId = ((GroupInfo) (parent.get(ShopCartListViewAdapter.SHOPCART_DATA))).getId();
for (int i = 0; i < list.size(); i++) {
Map map = list.get(i);
String child_parentId = (String) map.get(ShopCartListViewAdapter.SHOPCART_PARENT_ID);
if (parentId.equals(child_parentId)) {
ProductInfo pinfo = (ProductInfo) map.get(ShopCartListViewAdapter.SHOPCART_DATA);
pinfo.setChoosed(isChecked);
}
}
boolean allGroupSameState = true;
for (int i = 0; i < groups.size(); i++) {
if (isChecked != groups.get(i).isChoosed()) {
allGroupSameState = false;
break;
}
}
if (allGroupSameState) {
cb_check_all.setChecked(isChecked);
} else {
cb_check_all.setChecked(false);
}
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
@Override
public void checkChild(int position, boolean isChecked) {
Map child = list.get(position);
int parentPosition = (int) child.get(ShopCartListViewAdapter.SHOPCART_PARENT_POSITION);
GroupInfo parent = groups.get(parentPosition);
List childs = children.get(parent.getId());
boolean allChildSameState = true;
for (int i = 0; i < childs.size(); i++) {
if (childs.get(i).isChoosed() != isChecked) {
allChildSameState = false;
break;
}
}
if (allChildSameState) {
parent.setChoosed(isChecked);
} else {
parent.setChoosed(false);
}
boolean allGroupSameState = true;
boolean firstState = groups.get(0).isChoosed();
for (int i = 0; i < groups.size(); i++) {
if (firstState != groups.get(i).isChoosed()) {
allGroupSameState = false;
break;
}
}
if (allGroupSameState) {
cb_check_all.setChecked(firstState);
} else {
cb_check_all.setChecked(false);
}
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
@Override
public void doIncrease(int position, View showCountView, boolean isChecked) {
if (isChecked) {
Map map = list.get(position);
ProductInfo product = (ProductInfo) map.get(ShopCartListViewAdapter.SHOPCART_DATA);
int currentCount = product.getCount();
currentCount++;
product.setCount(currentCount);
((TextView)showCountView).setText(currentCount + "");
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
}
@Override
public void doDecrease(int position, View showCountView, boolean isChecked) {
if(isChecked){
Map map = list.get(position);
ProductInfo product = (ProductInfo) map.get(ShopCartListViewAdapter.SHOPCART_DATA);
int currentCount = product.getCount();
currentCount--;
if (currentCount < 0) {
currentCount = 0;
}
product.setCount(currentCount);
((TextView)showCountView).setText(currentCount + "");
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
}
/** 统计操作 */
private void calculateAll() {
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.getCount() * product.getPrice();
}
}
}
tv_total_price.setText("¥" + totalPrice);
tv_go_to_pay.setText("去结算(" + totalCount + ")");
}
/** 全选与反选 */
private void doCheckAll() {
for (int i = 0; i < groups.size(); i++) {
groups.get(i).setChoosed(cb_check_all.isChecked());
List childs = children.get(groups.get(i).getId());
for (int j = 0; j < childs.size(); j++) {
childs.get(j).setChoosed(cb_check_all.isChecked());
}
}
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
/**删除操作
* 注意:执行删除操作时,不可边遍历边删除,因为极有可能触发数组越界的错误
* 这里是在遍历的时候将要删除的元素放进相应的列表容器中,遍历完后,执行removeAll的方法来进行删除*/
private void doDelete(){
List> toBeDelete=new ArrayList<>();
List toBeDeleteGroups=new ArrayList<>();
for(int i=0;i childs=children.get(group.getId());
if(group.isChoosed()){
toBeDeleteGroups.add(group);
}
List toBeDeleteProducts=new ArrayList<>();
for(int j=0;jif(childs.get(j).isChoosed()){
toBeDeleteProducts.add(childs.get(j));
}
}
childs.removeAll(toBeDeleteProducts);//删除子元素列表
}
groups.removeAll(toBeDeleteGroups);//删除组元素列表
//再次遍历,删除源数据列表
for(int i=0;i//由于两种Item都继承BaseInfo,所以这里造型为BaseInfo,能比较方便地查看选中状态
BaseInfo info=(BaseInfo) list.get(i).get(ShopCartListViewAdapter.SHOPCART_DATA);
if(info.isChoosed()){
toBeDelete.add(list.get(i));
}
}
list.removeAll(toBeDelete);
sclva.notifyDataSetChanged();//刷新界面
calculateAll();//更新总价数量
}
@Override
public void onClick(View v) {
AlertDialog alert;
switch (v.getId()) {
case R.id.cb_check_all:
doCheckAll();
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;
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;
}
}
}
主Activity中会发现有三个数据列表。其中list是用于填充适配器的列表。groups是将list里的组元素提取出来的一个列表,children是将list中的子元素提取出来的一个列表。使用后两种列表,是为了便于遍历子元素。其实现方式很类似于ExpandableListView的构造方式。
使用ExpandableListView相比与ListView是简单的。因为ExpandableListView适配器的默认构造数据列表就是一个组元素的List和一个子元素的Map。从某种意义上来说,我更倾向与这种方式去实现购物车的功能。
package com.cjs.test.adapter;
import java.util.List;
import java.util.Map;
import com.cjs.test.entity.GroupInfo;
import com.cjs.test.entity.ProductInfo;
import com.cjs.test.shopcart_expandablelistview.R;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
/**
* ExpandableListView的购物车适配器
* @author chenjunsen
* 2015年8月18日下午8:53:57
*/
public class ShopcartExpandableListViewAdapter extends BaseExpandableListAdapter {
private List groups;
private Map> children;
private Context context;
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
/**
* 构造函数
* @param groups 组元素列表
* @param children 子元素列表
* @param context
*/
public ShopcartExpandableListViewAdapter(List groups, Map> children,
Context context) {
super();
this.groups = groups;
this.children = children;
this.context = context;
}
public void setCheckInterface(CheckInterface checkInterface) {
this.checkInterface = checkInterface;
}
public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
this.modifyCountInterface = modifyCountInterface;
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
String groupId = groups.get(groupPosition).getId();
return children.get(groupId).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List childs = children.get(groups.get(groupPosition).getId());
return childs.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder gholder;
if (convertView == null) {
gholder = new GroupHolder();
convertView = View.inflate(context, R.layout.item_shopcart_group, null);
gholder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
convertView.setTag(gholder);
} else {
gholder = (GroupHolder) convertView.getTag();
}
final GroupInfo group = (GroupInfo) getGroup(groupPosition);
if (group != null) {
gholder.tv_group_name.setText(group.getName());
gholder.cb_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
group.setChoosed(((CheckBox) v).isChecked());
checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());//暴露组选接口
}
});
gholder.cb_check.setChecked(group.isChoosed());
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
final ChildHolder cholder;
if (convertView == null) {
cholder = new ChildHolder();
convertView = View.inflate(context, R.layout.item_shopcart_product, null);
cholder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
cholder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
cholder.tv_product_name = (TextView) convertView.findViewById(R.id.tv_product_name);
cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_product_desc);
cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
cholder.iv_increase = (ImageView) convertView.findViewById(R.id.iv_increase);
cholder.iv_decrease = (ImageView) convertView.findViewById(R.id.iv_decrease);
cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_buy_count);
convertView.setTag(cholder);
} else {
cholder = (ChildHolder) convertView.getTag();
}
final ProductInfo product = (ProductInfo) getChild(groupPosition, childPosition);
if (product != null) {
cholder.tv_product_name.setText(product.getName());
cholder.tv_product_desc.setText(product.getDesc());
cholder.tv_price.setText("¥" + product.getPrice() + "");
cholder.tv_count.setText(product.getCount() + "");
cholder.cb_check.setChecked(product.isChoosed());
cholder.cb_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
product.setChoosed(((CheckBox) v).isChecked());
cholder.cb_check.setChecked(((CheckBox) v).isChecked());
checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());//暴露子选接口
}
});
cholder.iv_increase.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count,
cholder.cb_check.isChecked());//暴露增加接口
}
});
cholder.iv_decrease.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count,
cholder.cb_check.isChecked());//暴露删减接口
}
});
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
/**
* 组元素绑定器
* @author chenjunsen
* 2015年8月18日下午8:56:15
*/
private class GroupHolder {
CheckBox cb_check;
TextView tv_group_name;
}
/**
* 子元素绑定器
* @author chenjunsen
* 2015年8月18日下午8:56:25
*/
private class ChildHolder {
CheckBox cb_check;
ImageView iv_pic;
TextView tv_product_name;
TextView tv_product_desc;
TextView tv_price;
ImageView iv_increase;
TextView tv_count;
ImageView iv_decrease;
}
/**
* 复选框接口
* @author chenjunsen
* 2015年8月18日下午8:56:39
*/
public interface CheckInterface {
/**
* 组选框状态改变触发的事件
* @param groupPosition 组元素位置
* @param isChecked 组元素选中与否
*/
public void checkGroup(int groupPosition, boolean isChecked);
/**
* 子选框状态改变时触发的事件
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param isChecked 子元素选中与否
*/
public void checkChild(int groupPosition, int childPosition, boolean isChecked);
}
/**
* 改变数量的接口
* @author chenjunsen
* 2015年8月18日下午8:56:50
*/
public interface ModifyCountInterface {
/**
* 增加操作
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param showCountView 用于展示变化后数量的View
* @param isChecked 子元素选中与否
*/
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
/**
* 删减操作
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param showCountView 用于展示变化后数量的View
* @param isChecked 子元素选中与否
*/
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
}
}
package com.cjs.test.shopcart_expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cjs.test.adapter.ShopcartExpandableListViewAdapter;
import com.cjs.test.adapter.ShopcartExpandableListViewAdapter.CheckInterface;
import com.cjs.test.adapter.ShopcartExpandableListViewAdapter.ModifyCountInterface;
import com.cjs.test.entity.GroupInfo;
import com.cjs.test.entity.ProductInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* ExpandableListView实现的购物车
* @author chenjunsen
* 2015年8月18日下午8:31:24
*/
public class MainActivity extends Activity implements CheckInterface,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.cb_check_all);
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//关键步骤3,初始化时,将ExpandableListView以展开的方式呈现
}
cb_check_all.setOnClickListener(this);
tv_delete.setOnClickListener(this);
tv_go_to_pay.setOnClickListener(this);
}
/**模拟数据
* 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,
* 其键是组元素的Id(通常是一个唯一指定组元素身份的值)*/
private void virtualData(){
String[] groupNames = new String[] { "涛博运动", "巅峰运动专卖店", "阿迪达斯专卖店", "潮流前线", "鞋王精品店", "扬帆精品" };
String[] groupIds = new String[] { "shop1", "shop2", "shop3", "shop4", "shop5", "shop6" };
for(int i=0;inew GroupInfo();
group.setName(groupNames[i]);
group.setId(groupIds[i]);
groups.add(group);
List products=new ArrayList();
for(int j=0;j<=i;j++){
ProductInfo product=new ProductInfo();
product.setName("运动鞋" + (j + 1));
product.setDesc(groupNames[i] + "的鞋就是好,夏季促销,快来抢购!");
product.setPrice(199 + 50 * j);
product.setCount(1);
products.add(product);
}
children.put(groupIds[i], products);//将组元素的一个唯一值,这里取Id,作为子元素List的Key
}
}
@Override
public void onClick(View v) {
AlertDialog alert;
switch (v.getId()) {
case R.id.cb_check_all:
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;iif(group.isChoosed()){
toBeDeleteGroups.add(group);
}
List toBeDeleteProducts=new ArrayList();//待删除的子元素列表
List childs=children.get(group.getId());
for(int j=0;jif(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) {
if(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) {
if(isChecked){
ProductInfo product=(ProductInfo) selva.getChild(groupPosition, childPosition);
int currentCount=product.getCount();
currentCount--;
if(currentCount<0){
currentCount=0;
}
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@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;iif(childs.get(i).isChoosed()!=isChecked){
allChildSameState=false;
break;
}
}
if(allChildSameState){
group.setChoosed(isChecked);//如果所有子元素状态相同,那么对应的组元素被设为这种统一状态
}else{
group.setChoosed(false);//否则,组元素一律设置为未选中状态
}
selva.notifyDataSetChanged();
calculate();
}
/**全选与反选*/
private void doCheckAll(){
for(int i=0;i/**统计操作
* 1.先清空全局计数器
* 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作
* 3.给底部的textView进行数据填充*/
private void calculate(){
totalCount=0;
totalPrice=0.00;
for(int i=0;i childs=children.get(group.getId());
for(int j=0;jif(product.isChoosed()){
totalCount++;
totalPrice+=product.getPrice()*product.getCount();
}
}
}
tv_total_price.setText("¥"+totalPrice);
tv_go_to_pay.setText("去支付("+totalCount+")");
}
}
源码下载链接:
ExpandableListView版本http://download.csdn.net/detail/cjs1534717040/9024079
ListView版本http://download.csdn.net/detail/cjs1534717040/9024065