ExpandableListView的使用,Group和Child的类文件写法

    在网上找了很多ExpandableListView的用法和写法,不过看了大多对Group和Child的用法都是在Adapter里面写两个List集合代表Group和Child的数据,对此便是不理解,写安卓都会用到类文件,你写个List集合对于才开始用的人怎么会用,这些数据都是要存到数据库的,是要用到类来封装的,在此我贴上我自己的做法,也是集合网上的无奈,不说贴代码。

    Group类

public class PaymentGroup {

    //group的名字
    private String groupName;

    private List<PaymentFragmentInfo> list;

    public PaymentGroup(String groupName) {
        this.groupName = groupName;
        list = new ArrayList<PaymentFragmentInfo>();
    }

    //添加Child子类
    public void addUser(PaymentFragmentInfo info){
        list.add(info);
    }

    public int getChildCount(){
        return list.size();
    }

    //获取Child   在adapter的getChild方法时用到
    public PaymentFragmentInfo getChild(int childPosition){
        return list.get(childPosition);
    }

    /**
     * set  and  get   不多说
     * @return
     */
    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
}

Child类

@Table(name = "pay_info")
public class PaymentFragmentInfo {

    @Column(name = "id",isId = true,autoGen = true,property = "NOT NULL")
    private int id;

    @Column(name = "sheng")
    private String sheng;

    @Column(name = "name")
    private String name;

    @Column(name = "chepai")
    private String chepai;

    @Column(name = "jiashi")
    private String jiashi;

    @Column(name = "fakuan")
    private String fakuan;

    @Column(name = "zhina")
    private String zhina;

    public PaymentFragmentInfo() {
    }

    public PaymentFragmentInfo(String sheng, String name, String chepai, String jiashi, String fakuan, String zhina) {
        this.sheng = sheng;
        this.name = name;
        this.chepai = chepai;
        this.jiashi = jiashi;
        this.fakuan = fakuan;
        this.zhina = zhina;
    }  //  get  and  set  方法就不贴了

然后就是Adapter了  不过这里就要注意了getGroup和getChild方法  改成你写的Group类和Child类,下面是代码,注意看

public class PaymentFragmentAdapter extends BaseExpandableListAdapter {

    private List<PaymentGroup> list;
    private LayoutInflater inflater;

    public PaymentFragmentAdapter(Context context,List<PaymentGroup> list) {
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getChildCount();
    }

    /**
     * 这里的Object改成你自己的Group类文件
     */
    @Override
    public PaymentGroup getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    /**
     * 这里的Object改成你自己的Child类文件
     */
    @Override
    public PaymentFragmentInfo getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getChild(childPosition);
    }

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

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

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_payment_group,parent,false);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (GroupHolder) convertView.getTag();
        }

        PaymentGroup group = getGroup(groupPosition);
        holder.item_payment_group_text.setText(group.getGroupName());
//        holder.item_payment_group_checkbox.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //TODO  选择或者全选
//            }
//        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_payment_child,parent,false);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ChildHolder) convertView.getTag();
        }
        PaymentFragmentInfo info = getGroup(groupPosition).getChild(childPosition);
        holder.item_payment_child_sheng.setText(info.getSheng());
        holder.item_payment_child_sname.setText(info.getName());
        holder.item_payment_child_chepai.setText(info.getChepai());
        holder.item_payment_child_jiashi.setText(info.getJiashi());
        holder.item_payment_child_dakuan.setText(info.getFakuan());
        holder.item_payment_child_zhina.setText(info.getZhina());
        return convertView;
    }

    static class GroupHolder{
        CheckBox item_payment_group_checkbox,item_payment_group_checkbox1;
        TextView item_payment_group_text;
        public GroupHolder(View convertView){
            item_payment_group_checkbox = (CheckBox) convertView.findViewById(R.id.item_payment_group_checkbox);
            item_payment_group_checkbox1 = (CheckBox) convertView.findViewById(R.id.item_payment_group_checkbox1);
            item_payment_group_text = (TextView) convertView.findViewById(R.id.item_payment_group_text);
        }
    }

    static class ChildHolder{
        TextView item_payment_child_sheng,item_payment_child_sname,item_payment_child_chepai,
                item_payment_child_jiashi,item_payment_child_dakuan,item_payment_child_zhina;
        private ChildHolder(View convertView){
            item_payment_child_sheng = (TextView) convertView.findViewById(R.id.item_payment_child_sheng);
            item_payment_child_sname = (TextView) convertView.findViewById(R.id.item_payment_child_sname);
            item_payment_child_chepai = (TextView) convertView.findViewById(R.id.item_payment_child_chepai);
            item_payment_child_jiashi = (TextView) convertView.findViewById(R.id.item_payment_child_jiashi);
            item_payment_child_dakuan = (TextView) convertView.findViewById(R.id.item_payment_child_dakuan);
            item_payment_child_zhina = (TextView) convertView.findViewById(R.id.item_payment_child_zhina);
        }
    }
}

  这些注意一点  就没有什么的,然后是Activity里面了,这就没什么贴的了,就和ListView是一样的用法了,然后点击事件什么的,谢谢


如有不对  请纠正   谢谢  !!转载请注明出处




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值