Mono For Android 实现索引分段器效果

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值