編輯:關於Android編程
下面寫怎麼讓每個首字母相同的聯系人歸類展示:
在adapter implement SectionIndexer
這樣adapter裡必須實現以下3個接口:
@Override public Object[] getSections() { //section的集合 } @Override public int getPositionForSection(int section) {//當前section的位置 } @Override public int getSectionForPosition(int position) {//當前position的section信息 }
定義一個來存儲section的信息:
private class SectionTitle{ public String title; public SectionTitle(){ title = "\"; } public String toString(){ return title; } }
定義以下結構來獲取實現sectionIndexer所要的信息:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;"> private class ContactSectionMapper implements SectionIndexer {
private SectionTitle[] mSections = null;
private SparseIntArray mSectionPositionMap = null;
private SparseIntArray mPositionSectionMap = null;
public ContactSectionMapper(SectionTitle[] sectionDatas) {
mSections = sectionDatas;
}
public void changeData(SparseIntArray sectionPositionMap, SparseIntArray positionSectionMap) {
mSectionPositionMap = sectionPositionMap;
mPositionSectionMap = positionSectionMap;
}
@Override
public Object[] getSections() {
return mSections;
}
/**
* 找出這個section對應的position
*/
@Override
public int getPositionForSection(int section) {
if (mSectionPositionMap == null)
return -1;
if (section == 0)
return -1;
return mSectionPositionMap.get(section, -1);
}
/**
* 找出這個position對應的section
*/
@Override
public int getSectionForPosition(int position) {
if (mPositionSectionMap == null)
return -1;
if (position <= 0)
return 0;
return mPositionSectionMap.get(position, -1);
}
/**
* @param position
* @return
*/
public boolean isSection(int position) {
if (position == 0)
return true;
int sectionIdx = getSectionForPosition(position);
int sectionPosition = getPositionForSection(sectionIdx);
if (sectionIdx == -1 && sectionPosition == -1)
return false;
return (position == sectionPosition);
}
public String getSection(int position) {
if (mSections == null)
return NONE_ENGLISH_LETTER_TITLE;
int sectionIndex = getSectionForPosition(position);
if (sectionIndex < 0 || sectionIndex >= mSections.length)
return NONE_ENGLISH_LETTER_TITLE;
return mSections[sectionIndex].toString();
}
}
在adapter裡面我們重載changeCursor(每次cursor改變都會調用)這個方法,進行加工,取出數據的首字母信息
@Override public void changeCursor(Cursor c) { processCursor(c); super.changeCursor(c); } private void processCursor(Cursor c) { /** define some variables */ SparseIntArray sectionPositionMap = new SparseIntArray(); SparseIntArray positionSectionMap = new SparseIntArray(); for(int i = 0; i < mSectionDatas.length; i++){ mSectionDatas[i].title = "\"; } if (c == null "| c.getCount() == 0 || c.isClosed()){ mSectionMapper.changeData(sectionPositionMap, positionSectionMap); return; } String curtitle = "\"; int i = 0; int position = 0; while (c.moveToNext()) { position = c.getPosition(); String curLetter = getTitle(getDisplayName(c)); //看看當前的名字的第一個名字是什麼,是不是section的開頭 if (TextUtils.isEmpty(curtitle) "| !TextUtils.equals(curLetter, curtitle)) { mSectionDatas[i].title = curLetter; //這個當前的名字是section的開頭 sectionPositionMap.put(i, position); curtitle = curLetter; i++; } positionSectionMap.put(position, i - 1); } for(; i < mSectionDatas.length; i++){ mSectionDatas[i].title = curtitle; sectionPositionMap.put(i, position); } mSectionMapper.changeData(sectionPositionMap, positionSectionMap); }
protected final class ContactsAdapter extends ResourceCursorAdapter implements SectionIndexer,OnScrollListener { protected boolean mLoading = true; private ContactSectionMapper mSectionMapper = null; private SectionTitle[] mSectionDatas = null; private static final int SECTION_COUNT = 27; public ContactsAdapter(Context context) { super(context, R.layout.contacts_list_item_photo,null); mSectionDatas = new SectionTitle[SECTION_COUNT]; for (int i = 0; i < SECTION_COUNT; i++) { mSectionDatas[i] = new SectionTitle(); } mSectionMapper = new ContactSectionMapper(mSectionDatas); }
@Override public Object[] getSections() { return mSectionMapper.getSections(); } @Override public int getPositionForSection(int section) { return mSectionMapper.getPositionForSection(section); } @Override public int getSectionForPosition(int position) { return mSectionMapper.getSectionForPosition(position); }
轉載請注明來源http://blog.csdn.net/siyehuazhilian/article/details/41803059 Android Studio快捷
在Android native編寫代碼時,會經常接觸到sp、wp,sp並不是smart pointer的意思,而是strong point;wp就是weak point
1.一些BB上節我們把妹子圖片的數據來源從本地改成了解析Gank提供的接口數據,我們本節想對這個圖片加載類進行優化,比如加上顯示本地圖片的,另外還有一點就是緩存,我們現在
看了Android版QQ的自定義頭像功能,決定自己實現,隨便熟悉下android繪制和圖片處理這一塊的知識。先看看效果:思路分析:這個效果可以用兩個View來完成,上層V