开发者

Android ExpandableListView Child Headers

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 child开发者_运维知识库ren 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 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);
}

Thanks!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜