Skip to content

Instantly share code, notes, and snippets.

@lprichar
Created January 31, 2017 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lprichar/904ade6f1aaf96d172436bed3fb24d9b to your computer and use it in GitHub Desktop.
Save lprichar/904ade6f1aaf96d172436bed3fb24d9b to your computer and use it in GitHub Desktop.
Xamarin Android ExpandableListView Example
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Android.Views;
using Object = Java.Lang.Object;
namespace Droid.Sample
{
[Activity(Label = "Droid.Sample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private RelativeLayout _relativeLayout;
private ListView _listView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
AddViews();
ConstrainLayout();
SetContentView(_relativeLayout);
}
private void ConstrainLayout()
{
_relativeLayout.ConstrainLayout(() =>
_listView.Top == _relativeLayout.Top &&
_listView.Left == _relativeLayout.Left &&
_listView.Right == _relativeLayout.Right &&
_listView.Bottom == _relativeLayout.Bottom
);
}
private void AddViews()
{
_relativeLayout = ViewUtils.AddRelativeLayout(this);
_listView = GetListView(_relativeLayout);
}
private ListView GetListView(RelativeLayout parent)
{
var listView = new ExpandableListView(this);
parent.AddView(listView);
var items = new List<MyThing> {new MyThing("Item 1"), new MyThing("Item 1"), new MyThing("Item 1"), new MyThing("Item 2"), new MyThing("Item 3"), new MyThing("Item 3")};
listView.SetAdapter(new MyAdapter(this, items));
return listView;
}
}
public class MyThing
{
private static int _intCount = 0;
public MyThing(string name)
{
Name = name;
Id = _intCount++;
}
public int Id { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
}
public class MyAdapter : BaseExpandableListAdapter
{
private readonly Context _context;
private readonly List<MyThing> _myThings;
private List<IGrouping<string, MyThing>> _myThingsGrouped;
public MyAdapter(Context context, List<MyThing> myThings) : base()
{
_context = context;
_myThings = myThings;
_myThingsGrouped = myThings.GroupBy(i => i.Name).ToList();
}
public override Object GetChild(int groupPosition, int childPosition)
{
return null;
}
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount(int groupPosition)
{
return _myThingsGrouped[groupPosition].Count();
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var myThing = _myThingsGrouped[groupPosition].ElementAt(childPosition);
return new TextView(_context) { Text = myThing.Name + " (" + myThing.Id + ")"};
}
public override Object GetGroup(int groupPosition)
{
return null;
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var group = _myThingsGrouped[groupPosition];
return new TextView(_context)
{
Text = group.Key
};
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount => _myThingsGrouped.Count;
public override bool HasStableIds => true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment