Android UI--开源项目IndexableListView(字母索引),准备Android面试

setContentView(R.layout.main);

// 初始化一些数据

mItems = new ArrayList();

mItems.add(“Diary of a Wimpy Kid 6: Cabin Fever”);

mItems.add(“Steve Jobs”);

mItems.add(“Inheritance (The Inheritance Cycle)”);

mItems.add(“11/22/63: A Novel”);

mItems.add(“The Hunger Games”);

mItems.add(“The LEGO Ideas Book”);

mItems.add(“Explosive Eighteen: A Stephanie Plum Novel”);

mItems.add(“Catching Fire (The Second Book of the Hunger Games)”);

mItems.add(“Elder Scrolls V: Skyrim: Prima Official Game Guide”);

mItems.add(“Death Comes to Pemberley”);

mItems.add(“Diary of a Wimpy Kid 6: Cabin Fever”);

mItems.add(“Steve Jobs”);

mItems.add(“Inheritance (The Inheritance Cycle)”);

mItems.add(“11/22/63: A Novel”);

mItems.add(“The Hunger Games”);

mItems.add(“The LEGO Ideas Book”);

mItems.add(“Explosive Eighteen: A Stephanie Plum Novel”);

mItems.add(“Catching Fire (The Second Book of the Hunger Games)”);

mItems.add(“Elder Scrolls V: Skyrim: Prima Official Game Guide”);

mItems.add(“做作”);

mItems.add(“wokao”);

Collections.sort(mItems); // 排序

ContentAdapter adapter = new ContentAdapter(this,

android.R.layout.simple_list_item_1, mItems);

mListView = (IndexableListView) findViewById(R.id.listview);

mListView.setAdapter(adapter);

mListView.setFastScrollEnabled(true); // 设置快速滑动

}

private class ContentAdapter extends ArrayAdapter implements

SectionIndexer {

private String mSections = “#ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

public ContentAdapter(Context context, int textViewResourceId,

List objects) {

super(context, textViewResourceId, objects);

}

@Override

public int getPositionForSection(int section) {

// If there is no item for current section, previous section will be

// selected

// 如果当前部分没有item,则之前的部分将被选择

for (int i = section; i >= 0; i–) {

for (int j = 0; j < getCount(); j++) {

System.out.println(getCount());

if (i == 0) { // #

// For numeric section 数字

for (int k = 0; k <= 9; k++) {// 1…9

// 字符串第一个字符与1~9之间的数字进行匹配

if (StringMatcher.match(

String.valueOf(getItem(j).charAt(0)),

String.valueOf(k)))

return j;

}

} else { // A~Z

if (StringMatcher.match(

String.valueOf(getItem(j).charAt(0)),

String.valueOf(mSections.charAt(i))))

return j;

}

}

}

return 0;

}

@Override

public int getSectionForPosition(int position) {

return 0;

}

@Override

public Object[] getSections() {

String[] sections = new String[mSections.length()];

for (int i = 0; i < mSections.length(); i++)

sections[i] = String.valueOf(mSections.charAt(i));

return sections;

}

}

}

字符串匹配工具类

/*

  • Copyright 2011 woozzu

  • Licensed under the Apache License, Version 2.0 (the “License”);

  • you may not use this file except in compliance with the License.

  • You may obtain a copy of the License at

  • http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software

  • distributed under the License is distributed on an “AS IS” BASIS,

  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  • See the License for the specific language governing permissions and

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值