Download Code

In this tutorial you will learn about ExpandableListView.ExpandableListView is just like simple ListView.Mostly people says menu with submenu.

ExpandableListView

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels. Groups which can individually be expanded to show its children. The items come from theExpandableListAdapter associated with this view.

ExpandableListAdapter

An adapter that links a ExpandableListView with the underlying data. The implementation of this interface will provide access to the data of the children (categorized by groups), and also instantiate Views for children and groups.

BaseExpandableListAdapter

BaseExpandableListAdapter used to provide data and Views from some data to an expandable list view.

Create new android application project.After that create three layouts.

ExpandableListView Layout(Main.axml).

 

Expandablelistview,Expandablelistadapter android,List,Dictionary, baseexpandablelistadapter,Getchildview,GtegroupView, expandable listview android xamarin tutorial, how to design custom expandable listview android xamarin, how to collapse expandable lsitview group xamarin android, expandable listview with subchild items xamarin android,

Group Layout(group_item.xml).

Expandablelistview,Expandablelistadapter android,List,Dictionary, baseexpandablelistadapter,Getchildview,GtegroupView, expandable listview android xamarin tutorial, how to design custom expandable listview android xamarin, how to collapse expandable lsitview group xamarin android, expandable listview with subchild items xamarin android,

Child Layout(child_item.xml).

Expandablelistview,Expandablelistadapter android,List,Dictionary, baseexpandablelistadapter,Getchildview,GtegroupView, expandable listview android xamarin tutorial, how to design custom expandable listview android xamarin, how to collapse expandable lsitview group xamarin android, expandable listview with subchild items xamarin android,

Done with layouts.Now create new activity ExpandableListAdapter.java.Do the following steps.

  • Extend this class with BaseExpandableListAdapter class.
  • Implement all unimplemented methods of BaseExpandableListAdaper class.
  • Create a constructor of ExpandableListAdapter class.
  • Create String List,Dictionary to get groups and child.
  • Go to getChildView() and getGroupView() method and inflate layout of childs.

So you know to main modules of ExpandableListAdapter class.

ExpandableListAdapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace XamarinExpandableList
{
	[Activity(Label = "ExpandableListAdapter")]

	public class ExpandableListAdapter : BaseExpandableListAdapter
	{
		private Activity _context;
		private List _listDataHeader; // header titles
											  // child data in format of header title, child title
		private Dictionary<string, List> _listDataChild;

		public ExpandableListAdapter(Activity context, List listDataHeader, Dictionary<String, List> listChildData)
		{
			_context = context;
			_listDataHeader = listDataHeader;
			_listDataChild = listChildData;
		}
		//for cchild item view
		public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
		{
			return _listDataChild[_listDataHeader[groupPosition]][childPosition];
		}
		public override long GetChildId(int groupPosition, int childPosition)
		{
			return childPosition;
		}

		public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
		{
			string childText = (string)GetChild(groupPosition, childPosition);
			if (convertView == null)
			{
				convertView = _context.LayoutInflater.Inflate(Resource.Layout.child_item, null);
			}
			TextView txtListChild = (TextView)convertView.FindViewById(Resource.Id.lblListItem);
			txtListChild.Text = childText;
			return convertView;
		}
		public override int GetChildrenCount(int groupPosition)
		{
			return _listDataChild[_listDataHeader[groupPosition]].Count;
		}
		//For header view
		public override Java.Lang.Object GetGroup(int groupPosition)
		{
			return _listDataHeader[groupPosition];
		}
		public override int GroupCount
		{
			get
			{
				return _listDataHeader.Count;
			}
		}
		public override long GetGroupId(int groupPosition)
		{
			return groupPosition;
		}
		public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
		{
			string headerTitle = (string)GetGroup(groupPosition);

			convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.group_item, null);
			var lblListHeader = (TextView)convertView.FindViewById(Resource.Id.lblListHeader);
			lblListHeader.Text = headerTitle;

			return convertView;
		}
		public override bool HasStableIds
		{
			get
			{
				return false;
			}
		}
		public override bool IsChildSelectable(int groupPosition, int childPosition)
		{
			return true;
		}

		class ViewHolderItem : Java.Lang.Object
		{
		}
	}
}

Now go to MainActivity.cs class.Follow the steps.

  • Create string List,Map variables.
  • Create variable for ExpandableListView.
  • Refer xml ExpandableListView.
  • Create group and childern.
  • Set ExpandableListAdapter with group and children.
  • Finally call setAdapter to set list to ExpandableListView.

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;

namespace XamarinExpandableList
{
	[Activity(Label = "XamarinExpandableList", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{

		ExpandableListAdapter listAdapter;
		ExpandableListView expListView;
		List listDataHeader;
		Dictionary<string, List> listDataChild;
		int previousGroup = -1;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			SetContentView(Resource.Layout.Main);
			expListView = FindViewById(Resource.Id.lvExp);

			// Prepare list data
			FnGetListData();

			//Bind list
			listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
			expListView.SetAdapter(listAdapter);

			FnClickEvents();
		}
		void FnClickEvents()
		{
			//Listening to child item selection
			expListView.ChildClick += delegate (object sender, ExpandableListView.ChildClickEventArgs e)
			{
				Toast.MakeText(this, "child clicked", ToastLength.Short).Show();
			};

			//Listening to group expand
			//modified so that on selection of one group other opened group has been closed
			expListView.GroupExpand += delegate (object sender, ExpandableListView.GroupExpandEventArgs e)
			{

				if (e.GroupPosition != previousGroup)
					expListView.CollapseGroup(previousGroup);
				previousGroup = e.GroupPosition;
			};

			//Listening to group collapse
			expListView.GroupCollapse += delegate (object sender, ExpandableListView.GroupCollapseEventArgs e)
			{
				Toast.MakeText(this, "group collapsed", ToastLength.Short).Show();
			};

		}
		void FnGetListData()
		{
			listDataHeader = new List();
			listDataChild = new Dictionary<string, List>();

			// Adding child data
			listDataHeader.Add("Programming Languages");
			listDataHeader.Add("IDE Tools");
			listDataHeader.Add("Google Services");

			// Adding child data
			var programming = new List();
			programming.Add("C# Programming");
			programming.Add("PHP Programming");
			programming.Add("Java Programming");
			programming.Add("Python Programming");
			programming.Add("C Programming");
			programming.Add("Objective C Programming");
			programming.Add("C++ Programming");

			var ide = new List();
			ide.Add("Xamarin Studio");
			ide.Add("Android Studio");
			ide.Add("Eclipse");
			ide.Add("Xcode");
			ide.Add("Zend Studio");
			ide.Add("Flash Builder");

			var google = new List();
			google.Add("Google Cloud Messaging");
			google.Add("Google Maps");
			google.Add("Firebase Cloud Messaging");
			google.Add("Parse");
			google.Add("GCM Push Notification");
			google.Add("Recycler View");

			// Header, Child data
			listDataChild.Add(listDataHeader[0], programming);
			listDataChild.Add(listDataHeader[1], ide);
			listDataChild.Add(listDataHeader[2], google);
		}
	}
}

Run your android application project.

Expandablelistview,Expandablelistadapter android,List,Dictionary, baseexpandablelistadapter,Getchildview,GtegroupView, expandable listview android xamarin tutorial, how to design custom expandable listview android xamarin, how to collapse expandable lsitview group xamarin android, expandable listview with subchild items xamarin android,

That’s it for this tutorial.You can learn similar example in Java from Android Codec.

Thanks.