ListView和ExpandableListView实现安卓购物车功能

效果图
尝试用ListView和ExpandableListView两种方式来实现安卓购物车。结果发现了一些很奇妙的问题。

1.用ListView实现

当看见效果图上的东西时,很多人的第一印象是选择ListView来作为底层容器。用ListView的确可以实现。而且貌似扩展性比Expandable好。其ListView只有一个数据列表。该列表是一个由众多Map组成的List。无论是商品的还是商品所属的组,都是一个Map,不过在inflate视图的时候,会根据Map中的实现设置的flag进行选择性地加载相应的Item的layout。

(1)适配器

关键处在于重写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<Map<String, Object>> list;//源数据列表
    private Context context;
    private ModifyCountInterface modifyCountInterface;//自定义的改变商品数量的接口
    private CheckInterface checkInterface;//自定义的复选框状态变化的接口

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 构造函数
     * @param list
     * @param context
     */
    public ShopCartListViewAdapter(List<Map<String, Object>> list, Context context) {
        super();
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("CutPasteId")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        /*整体思路与普通ListView的适配器思路大致相同,不过,在绘制View的时候,根据Item的类型进行选择性绘制*/
        int type=getItemViewType(position);//获取Item的类型代码,代码自己定义,具体参照本适配器的末尾描述
        switch (type) {
        case SHOPCART_FLAG_GROUP:
            final 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();
            }

            @SuppressWarnings("unchecked")
            Map<String, Object> group=(Map<String, Object>) getItem(position);
            final GroupInfo groupinfo=(GroupInfo) group.get(SHOPCART_DATA);//根据绝对位置获取组元素
            if(groupinfo!=null){
                gholder.tv_group_name.setText(groupinfo.getName());
                /*设置组选框的点击事件
                 * 1.给对应的组元素设置选中与否的属性(内在的,实质的选中与否)
                 * 2.将相关的组元素信息通过自定义接口暴露出去,这里根据需求,暴露了绝对位置position和组选框的选中与否状态(视图上的选中与否)*/
                gholder.cb_check.setOnClickListener(new OnClickListener() {     
                    @Override
                    public void onClick(View v) {
                        groupinfo.setChoosed(gholder.cb_check.isChecked());
                        checkInterface.checkGroup(position, gholder.cb_check.isChecked());
                    }
                });
                gholder.cb_check.setChecked(groupinfo.isChoosed());//将实质的选中与否与视图的相结合,当对适配器进行notifyDataSetChanged操作时会有明显的效果
            }
            break;

        case SHOPCART_FLAG_CHILDREN:
            final ChildrenHolder cholder;
            if(convertView==null){
                cholder=new ChildrenHolder();
                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=(ChildrenHolder) convertView.getTag();
            }
            @SuppressWarnings("unchecked")
            Map<String, Object> children=(Map<String, Object>) getItem(position);
            final ProductInfo productInfo=(ProductInfo) children.get(SHOPCART_DATA);
            if(productInfo!=null){
                cholder.tv_product_name.setText(productInfo.getName());
                cholder.tv_product_desc.setText(productInfo.getDesc());
                cholder.tv_price.setText("¥"+productInfo.getPrice());
                cholder.tv_count.setText(productInfo.getCount()+"");//setText里不能放int等数值类型,否则运行时报错
                /*描述参照组选框*/
                cholder.cb_check.setOnClickListener(new OnClickListener() {         
                    @Override
                    public void onClick(View v) {
                        productInfo.setChoosed(cholder.cb_check.isChecked());
                        checkInterface.checkChild(position, cholder.cb_check.isChecked());
                    }
                });
                /*增加数量按钮*/
                cholder.iv_increase.setOnClickListener(new OnClickListener() {              
                    @Override
                    public void onClick(View v) {
                        boolean isChecked=cholder.cb_check.isChecked();
                        modifyCountInterface.doIncrease(position, cholder.tv_count, isChecked);
                    }
                });
                /*减少数量按钮*/
                cholder.iv_decrease.setOnClickListener(new OnClickListener() {      
                    @Override
                    public void onClick(View v) {
                        boolean isChecked=cholder.cb_check.isChecked();
                        modifyCountInterface.doDecrease(position, cholder.tv_count, isChecked);
                    }
                });
                cholder.cb_check.setChecked(productInfo.isChoosed());
            }
            break;
        }
        return convertView;
    }

    /*获取ListView的Item类型代号*/
    @Override
    public int getItemViewType(int position) {
        Map<String, Object> item=list.get(position);
        return (Integer) item.get(SHOPCART_TYPE);
    }
    /*获取ListView的Item的种类数量*/
    @Override
    public int getViewTypeCount() {
        return 2;//本适配器只有组和子两种元素的Item,所以返回2
    }
    /**
     * 组元素绑定器
     * @author chenjunsen
     * 2015年8月17日下午11:14:47
     */
    private class GroupHolder{
        CheckBox cb_check;
        TextView tv_group_name;
    }
    /**
     * 子元素绑定器
     * @author chenjunsen
     * 2015年8月17日下午11:14:30
     */
    private class ChildrenHolder{
        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月16日上午11:50:19
     */
    public interface ModifyCountInterface{
        /**
         * 增加操作
         * @param position 商品的绝对位置(在整个源数据list中的位置)
         * @param showCountView 用于展示操作后数量的view
         * @param isChecked 是否被选中
         */
        public void doIncrease(int position,View showCountView,boolean isChecked);
        /**
         * 减少操作
         * @param position 商品的绝对位置(在整个源数据list中的位置)
         * @param showCountView 用于展示操作后数量的view
         * @param isChecked 是否被选中
         */
        public void doDecrease(int position,View showCountView,boolean isChecked);
    }

    /**
     * 复选框操作接口
     * @author chenjunsen
     * 2015年8月16日上午11:50:01
     */
    public interface CheckInterface{
        /**
         * 组选框选中与否
         * @param position 组元素的绝对位置(在整个源数据list中的位置)
         * @param isChecked 组选框选中状态
         */
        public void checkGroup(int position,boolean isChecked);
        /**
         * 子选框选中与否
         * @param position 子元素的绝对位置(在整个源数据list中的位置)
         * @param isChecked 子选框选中状态
         */
        public void checkChild(int position,boolean isChecked);
    }

    /*********************************************************************************/
    /*下面的元素标志,取值是有讲究的。我曾经分别取到1,2。
     * 结果上下滑的时候会报数组越界。只要不取大于等于2的值,就不会报错。
     * 真心不明白这个常量标志为什么不能取大于等于2的值.
     * 猜测,2是Viewtype的种类,应该是标志不能超过或等于该值,具体原因不明*/
    /**组元素标志*/
    public static final int SHOPCART_FLAG_GROUP=0;
    /**子元素标志*/
    public static final int SHOPCART_FLAG_CHILDREN=1;
    /********************************************************************************/
    /**数据种类的键*/
    public static final String SHOPCART_TYPE="type";
    /**数据的键*/
    public static final String SHOPCART_DATA="data";
    /**子元素所属组元素的id*/
    public static final String SHOPCART_PARENT_ID="parent_id";
    /**子元素所属组元素的相对位置(在所有组元素中的位置)*/
    public static final String SHOPCART_PARENT_POSITION="parent_position";

}

适配器的关键之处,在于通过暴露相关接口(checkInterface和modifyInterface),使得组选和子选的操作得以在主Activity中进行,避免了使用全局静态变量的可能。

(2)主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<Map<String, Object>> list=new ArrayList<>();//源数据列表
    private List<GroupInfo> groups = new ArrayList<>();//组元素列表,对源数据列表进行二次选择封装,便于多选框选择变化时的操作
    private Map<String, List<ProductInfo>> 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<String, Object> 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<ProductInfo> childs = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                Map<String, Object> 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<String, Object> parent = list.get(position);
        String parentId = ((GroupInfo) (parent.get(ShopCartListViewAdapter.SHOPCART_DATA))).getId();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> 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<String, Object> child = list.get(position);
        int parentPosition = (int) child.get(ShopCartListViewAdapter.SHOPCART_PARENT_POSITION);
        GroupInfo parent = groups.get(parentPosition);
        List<ProductInfo> 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<String, Object> 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<String, Object> 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<ProductInfo> 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<ProductInfo> 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();//更新总价数量
    }

    /**删除操作
     * 注意:执行删除操作时,不可边遍历边删除,因为极有可能触发数组越界的错误<br>
     * 这里是在遍历的时候将要删除的元素放进相应的列表容器中,遍历完后,执行removeAll的方法来进行删除*/
    private void doDelete(){
        List<Map<String, Object>> toBeDelete=new ArrayList<>();
        List<GroupInfo> toBeDeleteGroups=new ArrayList<>();

        for(int i=0;i<groups.size();i++){
            GroupInfo group=groups.get(i);
            List<ProductInfo> childs=children.get(group.getId());
            if(group.isChoosed()){
                toBeDeleteGroups.add(group);        
            }       
            List<ProductInfo> toBeDeleteProducts=new ArrayList<>();
            for(int j=0;j<childs.size();j++){
                if(childs.get(j).isChoosed()){
                    toBeDeleteProducts.add(childs.get(j));
                }
            }
            childs.removeAll(toBeDeleteProducts);//删除子元素列表
        }
        groups.removeAll(toBeDeleteGroups);//删除组元素列表

        //再次遍历,删除源数据列表
        for(int i=0;i<list.size();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的构造方式。

2.用ExpandableListView实现

使用ExpandableListView相比与ListView是简单的。因为ExpandableListView适配器的默认构造数据列表就是一个组元素的List和一个子元素的Map。从某种意义上来说,我更倾向与这种方式去实现购物车的功能。

(1)适配器

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<GroupInfo> groups;
    private Map<String, List<ProductInfo>> children;
    private Context context;

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    /**
     * 构造函数
     * @param groups 组元素列表
     * @param children 子元素列表
     * @param context
     */
    public ShopcartExpandableListViewAdapter(List<GroupInfo> groups, Map<String, List<ProductInfo>> 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<ProductInfo> 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);
    }

}

(2)主Activity

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<GroupInfo> groups=new ArrayList<GroupInfo>();//组元素数据列表
    private Map<String, List<ProductInfo>> children=new HashMap<String, List<ProductInfo>>();//子元素数据列表
    @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<selva.getGroupCount();i++){
            exListView.expandGroup(i);//关键步骤3,初始化时,将ExpandableListView以展开的方式呈现
        }
        cb_check_all.setOnClickListener(this);
        tv_delete.setOnClickListener(this);
        tv_go_to_pay.setOnClickListener(this);
    }

    /**模拟数据<br>
     * 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,<br>
     * 其键是组元素的Id(通常是一个唯一指定组元素身份的值)*/
    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++){
            GroupInfo group=new GroupInfo();
            group.setName(groupNames[i]);
            group.setId(groupIds[i]);
            groups.add(group);

            List<ProductInfo> products=new ArrayList<ProductInfo>();
            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;
        }
    }
    /**删除操作<br>
     * 1.不要边遍历边删除,容易出现数组越界的情况<br>
     * 2.现将要删除的对象放进相应的列表容器中,待遍历完后,以removeAll的方式进行删除*/
    protected void doDelete() {
        List<GroupInfo> toBeDeleteGroups=new ArrayList<GroupInfo>();//待删除的组元素列表 
        for(int i=0;i<groups.size();i++){
            GroupInfo group=groups.get(i);
            if(group.isChoosed()){
                toBeDeleteGroups.add(group);
            }
            List<ProductInfo> toBeDeleteProducts=new ArrayList<ProductInfo>();//待删除的子元素列表
            List<ProductInfo> 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) {
        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<ProductInfo> childs=children.get(group.getId());
        for(int i=0;i<childs.size();i++){
            childs.get(i).setChoosed(isChecked);
        }
        selva.notifyDataSetChanged();
        calculate();
    }
    @Override
    public void checkChild(int groupPosition, int childPosiTion, boolean isChecked) {
        boolean allChildSameState=true;//判断改组下面的所有子元素是否是同一种状态
        GroupInfo group=groups.get(groupPosition);
        List<ProductInfo> 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);//否则,组元素一律设置为未选中状态
        }
        selva.notifyDataSetChanged();
        calculate();
    }

    /**全选与反选*/
    private void doCheckAll(){
        for(int i=0;i<groups.size();i++){
            groups.get(i).setChoosed(cb_check_all.isChecked());
            checkGroup(i, cb_check_all.isChecked());
        }
        selva.notifyDataSetChanged();
    }

    /**统计操作<br>
     * 1.先清空全局计数器<br>
     * 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作<br>
     * 3.给底部的textView进行数据填充*/
    private void calculate(){
        totalCount=0;
        totalPrice=0.00;
        for(int i=0;i<groups.size();i++){
            GroupInfo group=groups.get(i);
            List<ProductInfo> 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+")");
    }
}

源码下载链接:
ExpandableListView版本http://download.csdn.net/detail/cjs1534717040/9024079
ListView版本http://download.csdn.net/detail/cjs1534717040/9024065

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值