Here, admittedly, is a crude but functional example of using a custom extensible list adapter. You can think of a data source as a list of lists, as each item will expand to display a list of items below it. To represent this, we will use this simple model:
public class Group : Java.Lang.Object
{
public string Name { get; set; }
public IList<string> Items { get; set; }
}
, , BaseExpandableListAdapter /:
public class MyAdapter : BaseExpandableListAdapter
{
private readonly Context _context;
private readonly IList<Group> _groups;
public MyAdapter(Context context, IList<Group> groups)
{
_context = context;
_groups = groups;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
return _groups[groupPosition].Items[childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return (groupPosition * _groups.Count) + childPosition;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var view = (TextView)(convertView ?? new TextView(_context));
view.Text = _groups[groupPosition].Items[childPosition];
return view;
}
public override int GetChildrenCount(int groupPosition)
{
return _groups[groupPosition].Items.Count;
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return _groups[groupPosition];
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var view = (TextView)(convertView ?? new TextView(_context));
view.Text = _groups[groupPosition].Name;
return view;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount
{
get { return _groups.Count; }
}
public override bool HasStableIds
{
get { return true; }
}
}
, , .. , TextView, / , .
, , :
[Activity(Label = "ExpandableListDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MyExpandableListActivity : ExpandableListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var groups = new List<Group>
{
new Group
{
Name = "Group 1",
Items = new List<string> { "Item 1.1", "Item 1.2", "Item 1.3" }
},
new Group
{
Name = "Group 2",
Items = new List<string> { "Item 2.1", "Item 2.2", "Item 2.3" }
}
};
var adapter = new MyAdapter(this, groups);
SetListAdapter(adapter);
}
}