ExpandableList的用法

public class ShoppingCarNewAdapter extends BaseExpandableListAdapter {

    private Context mCtx;
    private ArrayList<ShopcarParentBean> shopcarList = new ArrayList<ShopcarParentBean>();
    private onParentSelectListener parentListener;
    private onItemSelectListener itemListener;

    public void setOnItemSelectListener(onItemSelectListener itemListener) {
        this.itemListener = itemListener;
    }

    public void setOnParentSelectListener(onParentSelectListener parentListener) {
        this.parentListener = parentListener;
    }

    public ShoppingCarNewAdapter(Context context, ArrayList<ShopcarParentBean> list) {
        this.mCtx = context;
        this.shopcarList = list;
    }

    public void adapterChange(ArrayList<ShopcarParentBean> list) {
        this.shopcarList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return shopcarList.get(groupPosition).getShopCarItem().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int position, boolean arg2, View view, ViewGroup arg4) {
        ChildViewHolder childVHolder = null;
        if (view == null) {
            view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_shopping_car, null);
            childVHolder = new ChildViewHolder(view);
            view.setTag(childVHolder);
        } else {
            childVHolder = (ChildViewHolder) view.getTag();
        }
        // 捆绑数据
        final ShopcarBean shopcarBean = (ShopcarBean) getChild(groupPosition, position);

        childVHolder.costTv.setText(shopcarBean.getCash());
        childVHolder.orderNoTv.setText(shopcarBean.getNum());
        if (shopcarList.get(groupPosition).isSelect() || shopcarBean.isSelect()) {
            childVHolder.checkBox.setChecked(true);
        } else {
            childVHolder.checkBox.setChecked(false);
        }
        childVHolder.checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CheckBox checkBox = (CheckBox) arg0;
                shopcarBean.setIsSelect(checkBox.isChecked());
                if (itemListener != null) {
                    itemListener.setItemSelect(shopcarBean, groupPosition);
                }

            }
        });
        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return shopcarList.get(groupPosition).getShopCarItem().size();// 二级分类的数量
    }

    @Override
    public Object getGroup(int arg0) {
        return shopcarList.get(arg0);
    }

    @Override
    public int getGroupCount() {
        return shopcarList.size();// 一级分类的数量
    }

    @Override
    public long getGroupId(int arg0) {
        return arg0;
    }

    @Override
    public View getGroupView(int postion, boolean arg1, View view, ViewGroup arg3) {
        ParentViewHolder parentVHolder = null;
        if (null == view) {
            view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_shopping_car_parent, null);
            parentVHolder = new ParentViewHolder(view);
            view.setTag(parentVHolder);
        } else {
            parentVHolder = (ParentViewHolder) view.getTag();
        }
        // 捆绑数据
        final ShopcarParentBean bean = shopcarList.get(postion);
        parentVHolder.shopNameTv.setText(bean.getShopName());
        parentVHolder.totalMoneyTv.setText(bean.getTotalMoney());
        if (bean.isSelect()) {
            parentVHolder.parentCheckBox.setChecked(true);
        } else {
            parentVHolder.parentCheckBox.setChecked(false);
        }
        parentVHolder.parentCheckBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CheckBox pBox = (CheckBox) arg0;
                bean.setIsSelect(pBox.isChecked());
                if (parentListener != null) {
                    parentListener.setParentSelect(bean);
                }
            }
        });

        return view;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

    static class ParentViewHolder {
        @Bind(R.id.adapter_shopping_car_total_money)
        TextView totalMoneyTv;
        @Bind(R.id.adapter_shop_car_name)
        TextView shopNameTv;
        @Bind(R.id.adapter_shopping_car_parent_check_box)
        CheckBox parentCheckBox;

        public ParentViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }

    static class ChildViewHolder {
        @Bind(R.id.adapter_shopping_car_orderno)
        TextView orderNoTv;
        @Bind(R.id.adapter_shopping_car_cost)
        TextView costTv;
        // @Bind(R.id.adapter_shopping_car_content_tv)
        // TextView contentTv;
        @Bind(R.id.adapter_shopping_car_check_box)
        CheckBox checkBox;

        public ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }

    public interface onParentSelectListener {
        public void setParentSelect(ShopcarParentBean shopCar);
    }

    public interface onItemSelectListener {
        public void setItemSelect(ShopcarBean shopCar, int parentPos);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值