请解释一下getPositionForSection(int)getSectionForPosition(int)getSections()以及ArrayAdapter方法的用法?

我对getViewTypeCount and getItemViewType methods of ArrayAdapter有了很好的解释。

谢谢。

最佳答案

Charsequence alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


这里

A = 0
B = 1
C = 2
D = 3
E = 4
F = 5
.
.
.
Z = 25


创建一个新的AlphabetIndexer

AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);


getSections()获取提供给AlphabetIndexer的字母的String []

String[] sections = (String[]) alphabetIndexer.getSections();


AlphabetIndexer.getSectionForPosition(position)检索当前位置的节

考虑给定的情况

position  Data             getSectionForPosition(position)
________  _________            __________

   0      Ajdhfj j             0
   1      Aadf hdsf            0
   2      Ajfkldsahf           0
   3      Asdhfa df            0
   4      Badhf                1
   5      Bdfj sadif           1
   6      Bghoi ij             1
   7      Bjkojg o             1
   8      Cadj fkljsdf         2
   9      Cgjds kfja           2
   10     Cn khdfaj            2
   11     Cph iohsdf           2
   12     Czjfa sh             2
   13     Dfgoa hjoifaj        3
   14     Dzfjdak sfh          3
   15     Fhf adhf             5
   16     Zdfh ajdf            25


获取职位职位

String section = sections[getSectionForPosition(position)];


AlphabetIndexer.getPositionForSection(section)检索数据从该部分开始的第一个位置

section      getPositionForSection(section)
_________    ____________________
0            0
1            4
2            8
3            13
4            13
5            15
6            15
7            15
8            15
.            .
.            .
.            .
23           15
24           15
25           16


希望这对您有所帮助。

使用AlphabetIndexer的示例

public abstract class SectionedListAdapter extends ResourceCursorAdapter {

private SparseIntArray sections;
private SparseIntArray cursorPositions;
private Context mContext;
private int sortedColumnIndex;

private static final int NORMAL_LIST_VIEW = 0;
private static final int SECTION_LIST_VIEW = 1;
private static final int NULL_LIST_VIEW = 2;

public SectionedListAdapter(Context context, int layout, Cursor c,
                            boolean autoRequery, int sortedColumnIndex, int defaultBitmapResId) {
    super(context, layout, c, autoRequery);
    this.mContext = context;
    this.sortedColumnIndex = sortedColumnIndex;
    setSortedCursorColumn(c);
}

public void setSortedCursorColumn(Cursor cursor){
    if (cursor == null){
        return;
    }
    AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    sections = new SparseIntArray();
    int m=0;
    int t = 'A';
    for (int i=0; i < 26; i++){
        if ((i+1) < 26) {
            int position = alphabetIndexer.getPositionForSection(i);
            int temp = alphabetIndexer.getPositionForSection(i + 1);
            if (temp != position){
                sections.put(position + m, t+i);
                m++;
            }
        } else {
            int position = alphabetIndexer.getPositionForSection(i);
            int temp = alphabetIndexer.getPositionForSection(i-1);
            if (position != temp){
                sections.put(position + m, t+i);
            }
        }
    }
    int temp = 0;
    cursorPositions = new SparseIntArray();
    for (int i=0; i<cursor.getCount() + sections.size(); i++){
        if (sections.get(i, -1) != -1){
            temp ++;
            cursorPositions.put(i, -1);
        } else {
            cursorPositions.put(i, i - temp);
        }
    }
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    if (getItemViewType(position) == NORMAL_LIST_VIEW){
        if (view == null){
            view = newView(mContext, getCursor(), parent);
        }
        getCursor().moveToPosition(cursorPositions.get(position));
        bindView(view, mContext, getCursor(), position);
    } else if (getItemViewType(position) == SECTION_LIST_VIEW) {
        if (view == null){
            view = newSectionView(mContext, getCursor(), parent);
        }
        bindSectionView(view, mContext, (char)sections.get(position));
    } else {
        if (view == null){
        } else {
            view = new ViewStub(mContext);
        }
    }
    return view;
}

public abstract View newSectionView(Context context, Cursor cursor, ViewGroup parent);

public abstract void bindSectionView(View view, Context context, char text);

public abstract void bindView(View view, Context context, Cursor cursor, int position);

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return cursorPositions.get(position) >= 0;
}

@Override
public int getItemViewType(int position) {
    if (cursorPositions.get(position) >= 0){
        return NORMAL_LIST_VIEW;
    }else if (cursorPositions.get(position) == -1){
        return SECTION_LIST_VIEW;
    } else {
        return NULL_LIST_VIEW;
    }
}

@Override
public int getViewTypeCount() {
    return 3;
}

private void putCursorExclusion(int position, boolean isActualPosition){
    if (isActualPosition) {
        cursorPositions.put(position, -cursorPositions.get(position));
    } else {
        int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(position));
        cursorPositions.put(actualPosition, -position);
    }
}

private void removeCursorExclusion(int position, boolean isActualPosition){
    if (isActualPosition){
        cursorPositions.put(position, -cursorPositions.get(position));
    } else {
        int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(-position));
        cursorPositions.put(actualPosition, position);
    }
}

@Override
public int getCount() {
    return cursorPositions == null ? 0 : cursorPositions.size();
}

public SparseIntArray getSections() {
    return sections;
}

public SparseIntArray getCursorPositions() {
    return cursorPositions;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //do nothing here
}

@Override
public Cursor swapCursor(Cursor newCursor) {
    return super.swapCursor(newCursor);
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public void changeCursor(Cursor cursor) {
    setSortedCursorColumn(cursor);
    super.changeCursor(cursor);
}
}


要使用此功能,只需创建一个新类扩展SectionedListAdapter,然后提供所需的详细信息即可。

关于android - ArrayAdapter的getPositionForSection和getSectionForPosition和getSections方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12438356/

10-09 07:11