android:layout_height=“fill_parent”
android:id="@+id/listview" />
Activity
package com.woozzu.android.indexablelistview;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import com.woozzu.android.util.StringMatcher;
import com.woozzu.android.widget.IndexableListView;
public class IndexableListViewActivity extends Activity {
private ArrayList mItems;
private IndexableListView mListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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
// 字