必要なのは次のとおりだと思います。
ExpandableListView、BaseExpandableListAdapter を拡張するアダプター、および groupView と子ビューの両方の対応するレイアウト。
BaseExpandableListAdapter を拡張すると、いくつかのメソッドを実装する必要がありますが、それらについて考えてみれば、それほど難しくはありません。
トリックはすべてアダプターの中にあります。親と子の両方のレイアウトをインフレートできます。私の例をチェックしてください:
このようにして、親ビューを親レイアウトで膨張させ、子も対応するレイアウトで膨張させます。それが役に立てば幸い。
public class ExpadableAdapter extends BaseExpandableListAdapter {
Activity context;
List<Movie> listObjects;
private static LayoutInflater inflaterParent = null;
private static LayoutInflater inflaterChild = null;
private ImageLoader imageLoader;
//on the constructor pass on the list of objects
public ExpadableAdapter(Activity context, List<Object> listObjects) {
context = context;
this.listObjects = listObjects;
inflaterParent = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflaterChild = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getChild(int groupPosition, int childPosition) {
//in this case I fetch the children on a DB, you can as well have a two dimensions array at first
List<Day> parentChildren = DBUtils.getObjectChildren(context, listObjects.get(groupPosition).getId());
return parentChildren.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Object object = listObjects.get(groupPosition);
//the first inflation for children views
View vi = convertView;
if(convertView==null)
vi = inflaterChild.inflate(R.layout.child_layout, null);
List<Day> listChildren = DBUtils.getChildren(context, listObjects.get(groupPosition).getId());
TextView textView = (TextView) vi.findViewById(R.id.child_layout_textview);
return vi;
}
public int getChildrenCount(int groupPosition) {
return listObjects.get(groupPosition).getCount();
}
public Object getGroup(int groupPosition) {
return listObjects.get(groupPosition);
}
public int getGroupCount() {
return listObjects.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View vi=convertView;
//the parent inflater.
if(convertView==null)
vi = inflaterParent.inflate(R.layout.parent_layout, null);
TextView textView=(TextView)vi.findViewById(R.id.parent_layout_text_view);;
textView.setText(listObjects.get(groupPosition).getName());
return vi;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}