1

getPositionForSection(int)の使用法と方法をgetSectionForPosition(int)説明してください。getSections()ArrayAdapter

ArrayAdapterのgetViewTypeCountメソッドとgetItemViewTypeメソッドについての良い説明がありました。

ありがとう。

4

1 に答える 1

6
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 を拡張し、必要な詳細を提供します。

于 2012-09-15T15:57:44.730 に答える