赞
踩
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。