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 が 0 以外の childPosition の R.layout.child_header になります。これにより、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);
}
ありがとう!