博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mono For Android 实现索引分段器效果
阅读量:5039 次
发布时间:2019-06-12

本文共 4858 字,大约阅读时间需要 16 分钟。

这两天看了个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            List
veges = 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这个接口。

转载于:https://www.cnblogs.com/rainmonth/p/3210391.html

你可能感兴趣的文章
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>
爬虫综合大作业
查看>>
Kubernetes 运维学习笔记
查看>>
并查集 经典 畅通工程
查看>>
Spark MLlib 之 Naive Bayes
查看>>
php修改SESSION的有效生存时间
查看>>
spring security 11种过滤器介绍
查看>>
Hibernate一对多、多对一关联
查看>>
一、记录Git使用中遇到的问题及解决方法
查看>>
学习网址
查看>>
前端表格插件datatables
查看>>
内部类
查看>>
树链剖分入门
查看>>
图解算法时间复杂度
查看>>
UI_搭建MVC
查看>>
一个样例看清楚JQuery子元素选择器children()和find()的差别
查看>>
代码实现导航栏分割线
查看>>
Windows Phone开发(7):当好总舵主 转:http://blog.csdn.net/tcjiaan/article/details/7281421...
查看>>
ASP.Net页面和控件缓存设置
查看>>