我需要在 ExpandableListView 中的展开子项的顶部添加一个标题。相同的标题应出现在所有展开的子项的顶部。我一直在尝试从这样的适配器中做到这一点:
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 将是 childPosition 处的 R.layout.child_header 而不是 0。这会中断 getTextLabel() 调用。
有没有更好的方法来做到这一点?我真正想要的是这样的:
myExpandableListView.addChildHeaderView(new ChildHeader())
我已经通过创建这样的视图而不是上面的视图解决了这个问题
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);
}
谢谢!