这两天看了个Demo,自己也动手写了下,觉得效果还不错,分享一下!
主要是利用ListActivity中自带了一个ListView和ListAdapter来实现的,当然也可以在一般的Activity中添加ListView和ListAdapter来实现。具体代码如下:
显示自定义的适配器的实现:
//HomeScreenAdapter.csusing 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{ 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 (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 Listveges = new List (); //利用流来打开文本文件 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 greensBitterleafBok choyBroccoli RabeBrussels sproutCabbageCatsearCeleryCeltuceCeylon spinachChardChayaChickweedChicoryChinese cabbageChinese MallowChrysanthemum leavesCollard greensCorn saladCressDandelionEndiveEpazoteFat henFiddleheadFluted pumpkinGarden RocketGolden samphireGood King HenryGreater PlantainKai-lanKaleKomatsunaKukaLagos bologiLamb's lettuceLand cressLettuceLizard's tailMelokhiaMizuna greensMustardNapa cabbageNew Zealand SpinachOrachePak choyParacressPea sprouts/leavesPokeRadicchioSamphireSea beetSea kaleSierra Leone bologiSokoSorrelSpinachSummer purslaneSwiss chardTatsoiTurnip greensWatercressWater spinachWinter purslaneYarrowAvocadoBell pepperBitter melon/Bitter gourdCourgetteCucumberIvy GourdEggplant/Aubergine/BrinjalPumpkinSquashSweet corn aka corn; aka maizeSweet pepperTindaTomatilloTomatoWinter melonWest Indian gherkinZucchiniArtichokeBroccoliCauliflowerCourgette flowersSquash blossomsAmerican groundnutAzuki beanBlack-eyed peaChickpeaCommon beanDrumstickDolichos beanFava beanGarbanzoGreen beanGuarGumboHorse gramIndian peaLentilLima BeanMoth beanMung beanOkraPeaPeanutPigeon peaRicebeanRunner beanSnap peaSnow peaSoybeanTarwiTepary beanUrad beanVelvet beanWinged beanYardlong beanAsparagusCardoonCeleriacCeleryElephant GarlicFlorence fennelGarlicKohlrabiKurratLeekLotus rootNopalOnionSpring Onion/ScallionPrussian asparagusShallotWelsh onionWild leekAhipaArracachaBamboo shootBeetrootBurdockBroadleaf arrowheadCamasCannaCarrotCassavaChinese artichokeDaikonEarthnut peaElephant Foot yamEnseteGingerGoboHamburg parsleyJerusalem artichokeJícamaManiocMooliParsnipPignutPlectranthusPotatoPrairie turnipRadishHorseradishRutabagaSalsifyScorzoneraSkirretSwedeSweet Potato or KumaraTaroTiTigernutTurnipUllucoWater chestnutYacónYam
关键部分就是如何实现ISectionIndexer这个接口。