通讯录或者事件列表需要用到此功能,先看截图e0f3bc3fe186590be43c2f24c0f07cb5.pnge73a356843c97eec14ade8391b15ae34.png首页源码

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 ArrayListmItems;

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("Death Comes to Pemberley");

mItems.add("Zeath Comes to Pemberley");

mItems.add("Xeath Comes to Pemberley");

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 ArrayAdapterimplements SectionIndexer {

private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public ContentAdapter(Context context, int textViewResourceId,

Listobjects) {

super(context, textViewResourceId, objects);

}

@Override

public int getPositionForSection(int section) {

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

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

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

if (i == 0) {

// For numeric section

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

if (StringMatcher.match(String.valueOf(getItem(j).charAt(0)), String.valueOf(k)))

return j;

}

} else {

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;

}

}

}

下载地址:IndexableListView-master

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐