使用字母索引的話,必須保證數據列表是按字母順序排序,以便AlphabetIndexer採用二分查找法快速定位
下面使用AlphabetIndexer來模擬快速滑動展示通訊錄。
自定義適配器,IndexAdapter,
public IndexAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// 設置數據遊標
// 設置索引字母列表
/**
* Cursor表示數據遊標 sortedColumnIndex按字母索引的遊標中的列號
* alphabet字母列表,用的最多的是"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
*/
alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,
"012346789");
// alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,
// "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,"012346789");
第一個參數:遊標,第二個參數:遊標中列號進行排序,第三個參數:快速展示的索引值
自定義遊標,獲取展示的數據
private class IndexCursor implements Cursor {
private ListAdapter adapter;
private int position;
private Map<String, String> map;
public IndexCursor(ListAdapter adapter) {
this.adapter = adapter;
}
@Override
public int getCount() {
return this.adapter.getCount();
}
/**
* 取得索引字母,這個方法非常重要,根據實際情況具體處理
*/
@SuppressWarnings("unchecked")
@Override
public String getString(int columnIndex) {
map = (HashMap<String, String>) adapter.getItem(position);
return map.get("itemText").substring(0, 2);
}
@Override
public boolean moveToPosition(int position) {
if (position < -1 || position > getCount()) {
return false;
}
this.position = position;
// 如果不滿意位置有點向上偏的話,下面這幾行代碼是修復定位索引值爲頂部項值的問題
// if(position+2>getCount()){
// this.position = position;
// }else{
// this.position = position + 2;
// }
return true;
}
@Override
public int getPosition() {
// TODO Auto-generated method stub
return 0;
}
在getString(int columnIndex)獲取去索引信息,按照索引排序和快速鎖定到對應的位置
<span style="font-family:SimSun;">public clas</span>s IndexMainActivity extends Activity {
private ListView tweakedListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setView();
initView();
setListener();
}
public void setView() {
setContentView(R.layout.activity_listview_index_main);
}
public void initView() {
tweakedListView = (ListView) findViewById(R.id.tweaked_list);
// 準備數據
List<Map<String, String>> itemList = getData();
ListAdapter adapter = new IndexAdapter(this, itemList,
R.layout.item_index_tweake_list_, new String[] { "itemText" },
new int[] { R.id.tweaked_item_text });
tweakedListView.setAdapter(adapter);
}
public void setListener() {
// TODO Auto-generated method stub
}
public List<Map<String, String>> getData() {
List<Map<String, String>> itemList = new ArrayList<Map<String, String>>();
String alphas = "0123456789";
// String alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Map<String, String> map = null;
for (char c : alphas.toCharArray()) {
for (int i = 0; i < 10; i++) {
map = new HashMap<String, String>();
map.put("itemText", "" + c + i);
itemList.add(map);
}
}
return itemList;
}
}
第一個參數:上下文 , 第二個參數:展示的數據map集合 ,第三個參數:item展示的佈局文件,第四個參數:map的鍵集合 ,第五個參數:item中條目的子控件id的集合
設置展示的數據List<Map<String,String>>,這裏是值是String,當然也可以是自定義的對象,這裏以String爲例,就使用SimpleAdapter適配器。
如果是item是其他的佈局,那麼就在對應的是適配器中添加AlphabetIndexer,讓其快速的滑動時,顯示對應的索引值。
源碼下載: http://download.csdn.net/detail/forwardyzk/8380273
效果圖: