전편 필요한 분.
2012/03/07 - [Android Study] - Android Study ListView Indexer 3편
PracticeIndexer class 내용 입니당.
public class PracticeIndexer implements SectionIndexer {
// Index 내용 초기화
// Index 내용 반환
// Index 위치 반환
// Index 위치 내용을 binarySearch 해서 배열 index 반환
이 코드역시 google base code를 따온것 이구요.
힘드니께 담 이야기는 담편으로~
2012/03/07 - [Android Study] - Android Study ListView Indexer 3편
PracticeIndexer class 내용 입니당.
// Index 내용
private final String[] mSections;
// Index 위치
// Index 위치
private final int[] mPositions;
// 최종 index 위치
// 최종 index 위치
private final int mCount;
public HistortSectionIndexer(String[] sections, int[] counts) {
// 생성자에서 Index 내용, 위치 배열 설정 null 이면 그냥 종료
// 생성자에서 Index 내용, 위치 배열 설정 null 이면 그냥 종료
if (sections == null || counts == null) {
throw new NullPointerException();
}
// Index 내용, 위치 배열 크기가 다르면 그냥 종료
if (sections.length != counts.length) {
throw new IllegalArgumentException(
"The sections and counts arrays must have the same length");
}
// Index 내용 초기화
this.mSections = sections;
// 위치는 같은 index를 사용 하는 것이 몇개 인지 counting 해서 전달 함.
// 그래서 count를 일일이 더해야 실제 위치가 나옴. 위치 배열 새로 생성
// 위치는 같은 index를 사용 하는 것이 몇개 인지 counting 해서 전달 함.
// 그래서 count를 일일이 더해야 실제 위치가 나옴. 위치 배열 새로 생성
mPositions = new int[counts.length];
int position = 0;
for (int i = 0; i < counts.length; i++) {
// Index 내용이 null 이면 공백으로 초기화
// Index 내용이 null 이면 공백으로 초기화
if (mSections[i] == null) {
mSections[i] = " ";
} else {
// Index 내용에 공백 있으면 제거
// Index 내용에 공백 있으면 제거
mSections[i] = mSections[i].trim();
}
// Index 위치 초기화
mPositions[i] = position;
// counting 된 값 더하기.
// counting 된 값 더하기.
position += counts[i];
}
// Index의 최종 위치 지정
// Index의 최종 위치 지정
mCount = position;
}
// Index 내용 반환
@Override
public Object[] getSections() {
return mSections;
}
// Index 위치 반환
@Override
public int getPositionForSection(int section) {
if (section < 0 || section >= mSections.length) {
return -1;
}
return mPositions[section];
}
// Index 위치 내용을 binarySearch 해서 배열 index 반환
@Override
public int getSectionForPosition(int position) {
if (position < 0 || position >= mCount) {
return -1;
}
int index = Arrays.binarySearch(mPositions, position);
/*
* Consider this example: section positions are 0, 3, 5; the supplied
* position is 4. The section corresponding to position 4 starts at
* position 3, so the expected return value is 1. Binary search will not
* find 4 in the array and thus will return -insertPosition-1, i.e. -3.
* To get from that number to the expected value of 1 we need to negate
* and subtract 2.
*/
// 위 코멘트는 Google 꺼... 내용인 즉슨 예를 들어 인덱스 위치가 0,3,5 이면
// 현재 위치가 4면 가까운 시작 위치가 3이니깐 1이 반환 되야함
// 근데 Binary search에 의해서 4가 나옴
// 그러니깐 -3이 반환되게 해서 4 + (-3) 하면 1이 나오게 됨.
// 위 코멘트는 Google 꺼... 내용인 즉슨 예를 들어 인덱스 위치가 0,3,5 이면
// 현재 위치가 4면 가까운 시작 위치가 3이니깐 1이 반환 되야함
// 근데 Binary search에 의해서 4가 나옴
// 그러니깐 -3이 반환되게 해서 4 + (-3) 하면 1이 나오게 됨.
return index >= 0 ? index : -index - 2;
} 이 코드역시 google base code를 따온것 이구요.
힘드니께 담 이야기는 담편으로~
'Android' 카테고리의 다른 글
Android Study ListView Indexer 6편 (3) | 2012.03.07 |
---|---|
Android Study ListView Indexer 5편 (0) | 2012.03.07 |
Android Study ListView Indexer 3편 (0) | 2012.03.07 |
Android Study ListView Indexer 2편 (1) | 2012.03.07 |
Android Study ListView Indexer 1편 (0) | 2012.03.07 |