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

ListView和ExpandableListView实现安卓购物车功能_第1张图片
尝试用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> 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> 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 group=(Map) 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 children=(Map) 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 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> 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的构造方式。

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 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);
    }

}

(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 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

你可能感兴趣的:(安卓)