Android的ExpandableListView孩子头 [英] Android ExpandableListView Child Headers

查看:109
本文介绍了Android的ExpandableListView孩子头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个头添加到扩大的孩子顶在ExpandableListView。相同的标题应该出现在所有扩展儿童的顶部。我一直在试图从这样的适配器做到这一点:

I need to add a header to the top of the expanded children in an ExpandableListView. The same header should appear at the top of all expanded children. I've been trying to do it from the adapter like this:

public int getChildrenCount(int groupPosition) {
 //Add one extra to children size
 return mGroups.get(groupPosition).size()+1;
}



 public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    //if childPosition 0 return the header
    if(childPosition == 0)return convertView = mInflater.inflate(R.layout.child_header, null);

    //childPosition -1 is the actual item
    ChildItem childItem = (ChildItem) getChild(groupPosition, childPosition-1);
    ChildViewHolder holder = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.child_row, null);
    } else {
    holder = (ChildViewHolder) convertView.getTag();
    }

    if(holder == null){
        holder = new ChildViewHolder(convertView);
        convertView.setTag(holder);
    }

    holder.getTextLabel().setText(childItem.name);
    holder.getPriceLabel().setText(String.valueOf(childItem.price));

    return convertView;
}

ChildViewHolder处理发现的子行的意见。这似乎是工作,但再随机convertView将是R.layout.child_header在childPosition 0以外这就打破getTextLabel()调用。

ChildViewHolder handles finding Views in the child row. This seems to work but then randomly convertView will be the R.layout.child_header at childPosition other than 0. This then breaks getTextLabel() call.

是否有这样做的更好的办法?我真正想要的是这样的:

Is there a better way of doing this? what I really want is something like this:

myExpandableListView.addChildHeaderView(new ChildHeader())

我通过创建这样的观点,而不是作为上述问题迎刃而解

I've solved the problem by creating the view like this instead of as above

if(convertView!=null){
    holder = (ChildViewHolder) convertView.getTag();
 }


if(holder == null){
    convertView = mInflater.inflate(R.layout.child_row, null);
    holder = new ChildViewHolder(convertView);
    convertView.setTag(holder);
}

谢谢!

推荐答案

抱歉这么晚才回复,这里是我的完整code

Sorry for the late reply, here is my complete code

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
            if(childPosition == 0)return convertView = mInflater.inflate(R.layout.child_header, null);

            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ChildViewHolder holder = null;

            ChildItem childItem = (ChildItem) getChild(groupPosition, childPosition-1);

            //Get ViewHolder first
            if(convertView!=null){
                holder = (ChildViewHolder) convertView.getTag();
            }

            //If no ViewHolder, then create a new child row as convertView is probably a header
            if(holder == null){
                convertView = mInflater.inflate(R.layout.child_row, null);
                holder = new ChildViewHolder(convertView);
                convertView.setTag(holder);
            }
            holder.getTextLabel().setText(childItem.name);
            holder.getPriceLabel().setText(String.valueOf(childItem.price));
            return convertView;
        }

这篇关于Android的ExpandableListView孩子头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆