Android 用于ArrayAdapter的getPositionForSection和getSectionForPosition和getSections方法
解释Android 用于ArrayAdapter的getPositionForSection和getSectionForPosition和getSections方法,android,Android,解释getPositionForSection(int)和getSectionForPosition(int)和getSections()的用法ArrayAdapter 我得到了一个很好的解释 谢谢 Charsequence alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 这里 创建新的字母索引器 AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex,
getPositionForSection(int)
和getSectionForPosition(int)
和getSections()的用法ArrayAdapter
我得到了一个很好的解释
谢谢
Charsequence alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
这里
创建新的字母索引器
AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
getSections()检索提供给字母索引器的字母表的字符串[]
AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
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)];
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 alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
公共抽象类SectionListAdapter扩展了ResourceCursorAdapter{
私人SparseIntArray路段;
私人SparseIntArray光标位置;
私有上下文;
私有int-sortedColumnIndex;
私有静态最终int正常列表视图=0;
私有静态最终整型部分列表视图=1;
私有静态final int NULL_LIST_VIEW=2;
公共分区ListAdapter(上下文上下文、int布局、游标c、,
布尔自动请求,int-sortedColumnIndex,int-defaultBitmapResId){
超级(上下文、布局、c、自动请求);
this.mContext=上下文;
this.sortedColumnIndex=sortedColumnIndex;
setSortedCursorColumn(c);
}
public void setSortedCursorColumn(光标){
if(游标==null){
返回;
}
字母索引器字母索引器=新的字母索引器(光标,分类列索引,“ABCDEFGHIJKLMNOPQRSTUVWXYZ”);
截面=新SparseIntArray();
int m=0;
int t='A';
对于(int i=0;i<26;i++){
如果((i+1)<26){
int position=字母索引器。getPositionForSection(i);
int temp=字母索引器getPositionForSection(i+1);
如果(温度!=位置){
放置(位置+m,t+i);
m++;
}
}否则{
int position=字母索引器。getPositionForSection(i);
int temp=字母索引器getPositionForSection(i-1);
如果(位置!=温度){
放置(位置+m,t+i);
}
}
}
内部温度=0;
cursorPositions=新SparseIntArray();
对于(int i=0;i=0;
}
@凌驾
public int getItemViewType(int位置){
if(cursorPositions.get(position)>=0){
返回正常列表视图;
}else if(cursorPositions.get(position)=-1){
返回节列表视图;
}否则{
返回空的列表视图;
}
}
@凌驾
public int getViewTypeCount(){
返回3;
}
私有void putCursorExclusion(int位置,布尔值isActualPosition){
如果(isActualPosition){
放置(位置,-cursorPositions.get(位置));
}否则{
int actualPosition=cursorPositions.keyAt(cursorPositions.indexOfValue(position));
放置(实际位置,-位置);
}
}
私有void removeCursorExclusion(int位置,布尔值isActualPosition){
如果(isActualPosition){
放置(位置,-cursorPositions.get(位置));
}否则{
int actualPosition=cursorPositions.keyAt(cursorPositions.indexOfValue(-position));
光标位置。放置(实际位置、位置);
}
}
@凌驾
public int getCount(){
返回cursorPositions==null?0:cursorPositions.size();
}
公共SparseIntArray getSections(){
回流段;
}
公共SparseIntArray getCursorPositions(){
返回光标位置;
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
//在这里什么也不做
}
@凌驾
公共游标交换游标(游标新建游标){
返回super.swapCursor(newCursor);
}
@凌驾
public void notifyDataSetChanged(){
super.notifyDataSetChanged();
}
@凌驾
公共void changeCursor(游标){
setSortedCursorColumn(光标);
super.changeCursor(游标);
}
}
要使用它,只需创建一个新类extensedSectionListAdapter,然后提供所需的详细信息
这里
创建新的字母索引器
AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
getSections()检索提供给字母索引器的字母表的字符串[]
AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
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)];
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 alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
String[] sections = (String[]) alphabetIndexer.getSections();
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);
}
}
公共抽象类SectionListAdapter扩展了ResourceCursorAdapter{
私人SparseIntArray路段;
私人SparseIntArray光标位置;
私有上下文;
私有int-sortedColumnIndex;
私有静态最终int正常列表视图=0;
私有静态最终整型部分列表视图=1;
私有静态final int NULL_LIST_VIEW=2;
公共分区ListAdapter(上下文上下文、int布局、游标c、,
布尔自动请求,int-sortedColumnIndex,int-defaultBitmapResId){
超级(上下文、布局、c、自动请求);
this.mContext=上下文;
this.sortedColumnIndex=sortedColumnIndex;
setSortedCursorColumn(c);
}
public void setSortedCursorColumn(光标){
if(游标==null){
返回;
}
字母索引器字母索引器=新的字母索引器(光标,分类列索引,“ABCDEFGHIJKLMNOPQRSTUVWXYZ”);
截面=新SparseIntArray();
int m=0;
int t='A';
对于(int i=0;i<26;i++){
如果((i+1)<26){
int position=字母索引器。getPositionForSection(i);
int temp=字母索引器getPositionForSection(i+1);
如果(温度!=位置){
放置(位置+m,t+i);