这两天看了个Demo,自己也动手写了下,觉得效果还不错,分享一下!
主要是利用ListActivity中自带了一个ListView和ListAdapter来实现的,当然也可以在一般的Activity中添加ListView和ListAdapter来实现。具体代码如下:
显示自定义的适配器的实现:
//HomeScreenAdapter.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 ListViewAdapter { [Activity(Label = "ListViewAdapter")] public class HomeScreenAdapter : BaseAdapter<string> { private string[] items; private Activity context; public HomeScreenAdapter(Activity context, string[] items) : base() { this.context = context; this.items = items; } public override string this[int position] { get { return items[position]; } } public override int Count { get { return items.Length; } } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null); } view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position]; return view; } } }
自定义适配器实现之后,只要将它赋给ListActivity中ListAdapter成员就可以了。
下面看看主类HomeScreen.cs的实现:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SectionIndex
{
[Activity(Label = "SectionIndex", MainLauncher = true, Icon = "@drawable/icon")]
public class HomeScreen : ListActivity
{
private string[] items;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//用于存放文本文件的List
List<string> veges = new List<string>();
//利用流来打开文本文件
Stream seedDataStream = Assets.Open(@"VegeData.txt");
//将文本文件内容读入veges中
//StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(seedDataStream))
{
while (!reader.EndOfStream)
{
veges.Add(reader.ReadLine());
}
}
//将veges中的元素进行排序,比较器为匿名函数构成的委托
veges.Sort((x,y)=>{ return x.CompareTo(y); } );
//将列表中的元素存入数组中
items = veges.ToArray();
}
protected override void OnListItemClick(ListView l, View v, int position, long id)
{
var t = items[position];
Toast.MakeText(this, t, ToastLength.Short).Show();
Console.WriteLine("Clicked on: " + t);
}
}
}
其中VegesData.txt为要读取的文本文件:其内容如下:
Beet greens
Bitterleaf
Bok choy
Broccoli Rabe
Brussels sprout
Cabbage
Catsear
Celery
Celtuce
Ceylon spinach
Chard
Chaya
Chickweed
Chicory
Chinese cabbage
Chinese Mallow
Chrysanthemum leaves
Collard greens
Corn salad
Cress
Dandelion
Endive
Epazote
Fat hen
Fiddlehead
Fluted pumpkin
Garden Rocket
Golden samphire
Good King Henry
Greater Plantain
Kai-lan
Kale
Komatsuna
Kuka
Lagos bologi
Lamb's lettuce
Land cress
Lettuce
Lizard's tail
Melokhia
Mizuna greens
Mustard
Napa cabbage
New Zealand Spinach
Orache
Pak choy
Paracress
Pea sprouts/leaves
Poke
Radicchio
Samphire
Sea beet
Sea kale
Sierra Leone bologi
Soko
Sorrel
Spinach
Summer purslane
Swiss chard
Tatsoi
Turnip greens
Watercress
Water spinach
Winter purslane
Yarrow
Avocado
Bell pepper
Bitter melon/Bitter gourd
Courgette
Cucumber
Ivy Gourd
Eggplant/Aubergine/Brinjal
Pumpkin
Squash
Sweet corn aka corn; aka maize
Sweet pepper
Tinda
Tomatillo
Tomato
Winter melon
West Indian gherkin
Zucchini
Artichoke
Broccoli
Cauliflower
Courgette flowers
Squash blossoms
American groundnut
Azuki bean
Black-eyed pea
Chickpea
Common bean
Drumstick
Dolichos bean
Fava bean
Garbanzo
Green bean
Guar
Gumbo
Horse gram
Indian pea
Lentil
Lima Bean
Moth bean
Mung bean
Okra
Pea
Peanut
Pigeon pea
Ricebean
Runner bean
Snap pea
Snow pea
Soybean
Tarwi
Tepary bean
Urad bean
Velvet bean
Winged bean
Yardlong bean
Asparagus
Cardoon
Celeriac
Celery
Elephant Garlic
Florence fennel
Garlic
Kohlrabi
Kurrat
Leek
Lotus root
Nopal
Onion
Spring Onion/Scallion
Prussian asparagus
Shallot
Welsh onion
Wild leek
Ahipa
Arracacha
Bamboo shoot
Beetroot
Burdock
Broadleaf arrowhead
Camas
Canna
Carrot
Cassava
Chinese artichoke
Daikon
Earthnut pea
Elephant Foot yam
Ensete
Ginger
Gobo
Hamburg parsley
Jerusalem artichoke
Jícama
Manioc
Mooli
Parsnip
Pignut
Plectranthus
Potato
Prairie turnip
Radish
Horseradish
Rutabaga
Salsify
Scorzonera
Skirret
Swede
Sweet Potato or Kumara
Taro
Ti
Tigernut
Turnip
Ulluco
Water chestnut
Yacón
Yam
关键部分就是如何实现ISectionIndexer这个接口。