Does anyone have an examples for using the BaseExpandableListAdapter with Mono for Android. I'm trying to implement this for one of my views, but am having issues with finding something that is thorough. Can anyone provide any examples on how they got this working with Mono for Android?
Here's an admittedly crude looking but functional example of using a custom expandable list adapter. You can think of the data source as being a list of lists, since each item will expand to display a list of items underneath it. To represent that, we'll use this simple model:
public class Group : Java.Lang.Object
{
public string Name { get; set; }
public IList<string> Items { get; set; }
}
Using that model you can then create a class that inherits from BaseExpandableListAdapter and implements all the required methods/properties:
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; }
}
}
The constructor for the adapter takes in a list of groups, and uses that to implement the methods that ask for a group, item, etc. To keep things simple, for each view I'm just rendering a single TextView but you can create/inflate any view you want for the items.
To demonstrate this in action, here's a sample activity that will load up an expandable list with some data:
[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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With