Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created June 11, 2013 14:27
Show Gist options
  • Save Cheesebaron/5757277 to your computer and use it in GitHub Desktop.
Save Cheesebaron/5757277 to your computer and use it in GitHub Desktop.
using Android.Views;
using Android.Widget;
namespace ListViewWithHeaders.Items
{
public class Header : IItem
{
public string HeaderName { get; set; }
public View GetView(LayoutInflater inflater, View convertView)
{
var view = convertView ?? inflater.Inflate(Resource.Layout.Header, null);
var text = view.FindViewById<TextView>(Resource.Id.separator);
text.Text = HeaderName;
return view;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+id/separator"
android:text="Header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#757678"
android:textColor="#f5c227" />
</LinearLayout>
using Android.Views;
namespace ListViewWithHeaders.Items
{
public interface IItem
{
View GetView(LayoutInflater inflater, View convertView);
}
}
using Android.Views;
using Android.Widget;
namespace ListViewWithHeaders.Items
{
public class Item : IItem
{
public string ItemName { get; set; }
public string SubName { get; set; }
public View GetView(LayoutInflater inflater, View convertView)
{
var view = convertView ?? inflater.Inflate(Resource.Layout.Item, null);
var itemText = view.FindViewById<TextView>(Resource.Id.list_content1);
var itemSubText = view.FindViewById<TextView>(Resource.Id.list_content2);
itemText.Text = ItemName;
itemSubText.Text = SubName;
return view;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/list_content1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dip"
android:clickable="false"
android:gravity="center"
android:longClickable="false"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:text="sample"
android:textColor="#ff7f1d"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/list_content2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dip"
android:clickable="false"
android:gravity="center"
android:linksClickable="false"
android:longClickable="false"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:text="sample"
android:textColor="#6d6d6d"
android:textSize="17dip" />
</LinearLayout>
using System.Collections.Generic;
using Android.Content;
using Android.Views;
using Android.Widget;
using ListViewWithHeaders.Items;
namespace ListViewWithHeaders.Adapters
{
public class ItemsWithHeaderListAdapter : BaseAdapter<IItem>
{
private readonly LayoutInflater _inflater;
public List<IItem> Items { get; private set; }
public ItemsWithHeaderListAdapter(Context context)
{
_inflater = LayoutInflater.FromContext(context);
Items = new List<IItem>();
}
public override long GetItemId(int position) { return position; }
public override View GetView(int position, View convertView, ViewGroup parent)
{
return Items[position].GetView(_inflater, convertView);
}
public override int Count { get { return Items.Count; } }
public override IItem this[int position] { get { return Items[position]; } }
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
using Android.App;
using Android.Widget;
using Android.OS;
using ListViewWithHeaders.Adapters;
using ListViewWithHeaders.Items;
namespace ListViewWithHeaders
{
[Activity(Label = "ListViewWithHeaders", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var listView = FindViewById<ListView>(Resource.Id.listView);
var adapter = new ItemsWithHeaderListAdapter(this);
listView.Adapter = adapter;
adapter.Items.Add(new Header { HeaderName = "Header 1"});
adapter.Items.Add(new Item { ItemName = "Text 1", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 2", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 3", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 4", SubName = "Rabble rabble" });
adapter.Items.Add(new Header { HeaderName = "Header 2" });
adapter.Items.Add(new Item { ItemName = "Text 5", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 6", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 7", SubName = "Rabble rabble" });
adapter.Items.Add(new Item { ItemName = "Text 8", SubName = "Rabble rabble" });
listView.PostInvalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment