To create your first app in Xamarin.Android, you can refer here.

Introduction

To create a ListView with headers, we are using an interface and enum.

Step 1

Open Visual Studio, and go to New Project >> Templates >> Visual C# >> Android, then select Blank App (Android)

Give project a name and choose location.

SharePoint

Step 2

Next, open Solution Explorer >> Project Name >> Resources >> layout, and then select Main.axml. After that, click "Open Design View".

SharePoint

Step 3

Go to Toolbar, select ListView, drag and drop in Design Page and then change id as android:id="@+id/listView1".

SharePoint


This is the code for List View.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:minWidth="25px"
  8. android:minHeight="25px">

  9. <ListView
  10. android:minWidth="25px"
  11. android:minHeight="25px"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:id="@+id/listView1" />
  15. </LinearLayout>

Step 4

Next, create the Item Template that is to be added in the list view to show the content.

Create MyTemplate.xaml for Item Template.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="Profile support"
  9. android:textSize="16dp"
  10. android:id="@+id/lbltransparent"
  11. android:paddingTop="10dp"
  12. android:paddingLeft="10dp"
  13. android:paddingRight="10dp" />
  14. </LinearLayout>
Step 5

Create an Enum that holds the type of view that's to be created.
ViewType.cs
  1. public enum ViewType
  2. {
  3. Header, List, Button
  4. }

Step 6

Create an interface that has a method.

IViewItemType .cs
  1. public interface IViewItemType
  2. {
  3. ViewType GetViewType();
  4. }

Step 7

Create three classes that inherit the above interface.

SettingsContent.cs
  1. public class SettingsContent : IViewItemType
  2. {
  3. public int ImageResource, ImageArrow;
  4. public string SettingsText;
  5. public SettingsContent(int imageResource,string text,int imgArrow)
  6. {
  7. ImageResource = imageResource;
  8. ImageArrow = imgArrow;
  9. SettingsText = text;
  10. }
  11. public ViewType GetViewType()
  12. {
  13. return ViewType.List;
  14. }
  15. }
HeaderText.cs
  1. public class HeaderText : IViewItemType
  2. {
  3. public string Text;
  4. public HeaderText(string text)
  5. {
  6. Text = text;
  7. }
  8. public ViewType GetViewType()
  9. {
  10. return ViewType.Header;
  11. }
  12. }
SettingsLogOut.cs
  1. public class SettingsLogOut : IViewItemType
  2. {
  3. public ViewType GetViewType()
  4. {
  5. return ViewType.Button;
  6. }
  7. }
Step 8

Create an activity that sets the adapter to the list view.
ListActivity.cs
  1. public class MainActivity : AppCompatActivity
  2. {
  3. public ListView SettingsListView;
  4. private List<IViewItemType> settingMenuContents;
  5. public MainActivity()
  6. {
  7. settingMenuContents = new List<IViewItemType>();
  8. settingMenuContents.Add(new HeaderText("ListView with Sections"));
  9. settingMenuContents.Add(new SettingsContent("BLUETOOTHNAME"));
  10. settingMenuContents.Add(new SettingsContent("ACTIVITIES"));
  11. settingMenuContents.Add(new SettingsContent("DEVICES"));
  12. settingMenuContents.Add(new SettingsContent("SECURITY"));
  13. settingMenuContents.Add(new HeaderText(string.Empty));
  14. settingMenuContents.Add(new SettingsContent("SUPPORT"));
  15. settingMenuContents.Add(new SettingsContent("REPORTPROBLEM"));
  16. settingMenuContents.Add(new SettingsContent("TERMS"));
  17. settingMenuContents.Add(new SettingsContent("ABOUT"));
  18. settingMenuContents.Add(new SettingsLogOut());
  19. }
  20. protected override void OnCreate(Bundle savedInstanceState)
  21. {
  22. base.OnCreate(savedInstanceState);
  23. SetContentView(Resource.Layout.activity_main);
  24. SettingsListView = FindViewById<ListView>(Resource.Id.sectionalListView);
  25. SettingsListView.Adapter = new ListAdapter(this, settingMenuContents);
  26. SettingsListView.Clickable = false;
  27. }
  28. }
Step 9

Create the adapter for list view in ListAdapter.cs.
  1. public class ListAdapter : BaseAdapter
  2. {
  3. Context context;
  4. public List<IViewItemType> SettingsItem;
  5. private LayoutInflater inflater;
  6. public ListAdapter(Context context, List<IViewItemType> items)
  7. {
  8. this.context = context;
  9. SettingsItem = items;
  10. inflater = (LayoutInflater)this.context.GetSystemService(Context.LayoutInflaterService);
  11. }
  12. public override Java.Lang.Object GetItem(int position)
  13. {
  14. return position;
  15. }
  16. public override long GetItemId(int position)
  17. {
  18. return position;
  19. }
  20. public override View GetView(int position, View convertView, ViewGroup parent)
  21. {
  22. View view = convertView;
  23. try
  24. {
  25. IViewItemType viewItem = SettingsItem[position];
  26. if (viewItem.GetViewType() == ViewType.Header)
  27. {
  28. view = inflater.Inflate(Resource.Layout.SettingsTextView, null);
  29. }
  30. else if (viewItem.GetViewType() == ViewType.List)
  31. {
  32. SettingsContent listViewContent = (SettingsContent)viewItem;
  33. view = inflater.Inflate(Resource.Layout.SettingsRowTemplate, null);
  34. //listview text
  35. view.FindViewById<TextView>(Resource.Id.lblSettingsText).Text = listViewContent.SettingsText;
  36. }
  37. else if (viewItem.GetViewType() == ViewType.Button)
  38. {
  39. //var gradient = (SettingsLogOut)item;
  40. view = inflater.Inflate(Resource.Layout.LogOutButton, null);
  41. }
  42. }
  43. catch
  44. {
  45. }
  46. return view;
  47. }
  48. //Fill in cound here, currently 0
  49. public override int Count
  50. {
  51. get
  52. {
  53. return SettingsItem.Count;
  54. }
  55. }
  56. }
That's all. Just run the app and you will see an output as below.
SharePoint
For reference, I have attached the zip file.
Hope you enjoy Xamarin.Android.