SectionIndexer
을 구현하는 ListView
용 어댑터를 사용하고 있습니다. ListView
은 fastScrollEnabled
이 xml 파일에서 true로 설정되어 있습니다. Android 2.2 및 2.3에서는 모든 것이 잘 작동하지만 Android 3.0 태블릿에서 응용 프로그램을 테스트 할 때 일부 섹션에서 스크롤 막대가 사라집니다. 예를 들어 목록을 아래로 스크롤하면 문자 A-B 스크롤바로 시작하는 요소가 표시되지만 문자 C-H에는 표시되지 않고 다시 H로 표시됩니다.HoneyComb의 특정 섹션에서 SectionIndexer를 사용하면 스크롤 막대가 사라집니다.
이 어댑터는 빠른 스크롤을 사용할 수 있도록 ListView에서 사전 순으로 내용을 정렬하기 위해 만들어졌습니다.
응용 프로그램은 API 레벨 8에 맞게 설계되었으므로 fastScrollAlwaysVisible을 사용할 수 없습니다. "나는 키"A "문자로 시작 6 개 요소, 값이 너무 때
public class AlphabetSimpleAdapter extends SimpleAdapter implements SectionIndexer {
private HashMap<String, Integer> charList;
private String[] alphabet;
public Typeface tfSansMedium;
public Context mContext;
public int mResource;
public int[] mTo;
public List<? extends Map<String, ?>> mData;
public String mTitleKey;
public AlphabetSimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to, String titleKey /* key sent in hashmap */) {
super(context, data, resource, from, to);
mData = data;
mTitleKey = titleKey;
mContext = context;
mResource = resource;
mTo = new int[to.length];
for (int i = 0; i < to.length; i ++)
{
mTo[i] = to[i];
}
charList = new HashMap<String, Integer>();
int size = data.size();
tfSansMedium = Typeface.createFromAsset(context.getAssets(), "fonts/VitesseSans-Medium.otf");
for(int i = 0; i < size; i++) {
// Parsing first letter of hashmap element
String ch = data.get(i).get(titleKey).toString().substring(0, 1);
ch = ch.toUpperCase();
if(!charList.containsKey(ch)) {
charList.put(ch, i); // Using hashmap to avoid duplicates
}
}
Set<String> sectionLetters = charList.keySet(); // A set of all first letters
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); // Creating arraylist to be able to sort elements
Collections.sort(sectionList, Collator.getInstance(new Locale("pl", "PL"))); // Sorting elements
alphabet = new String[sectionList.size()];
sectionList.toArray(alphabet);
}
// Methods required by SectionIndexer
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(mResource, null);
}
for (int i = 0; i < mTo.length; i ++) {
TextView tv = (TextView)v.findViewById(mTo[i]);
if (tv != null) tv.setTypeface(tfSansMedium);
}
return super.getView(position, v, parent);
}
@Override
public int getPositionForSection(int section) {
if(!(section > alphabet.length-1)) {
return charList.get(alphabet[section]);
}
else {
return charList.get(alphabet[alphabet.length-1]);
}
}
@Override
public int getSectionForPosition(int position) {
return charList.get(mData.get(position).get(mTitleKey).toString().substring(0, 1));
}
@Override
public Object[] getSections() {
return alphabet;
}
}
charList
내가 마지막에 나타나는 인덱스 문자를 저장할 수는 HashMap이다 : 여기
alphabet
은 기존의 모든 첫 번째 문자가있는 문자열 배열입니다.
알아 냈습니까? 나는 같은 문제에 맞붙고있다. 내 문제는 getSectionForPosition 메서드로 발생했습니다. – KickingLettuce
불행히도 아닙니다. 이것을 알아내는 데 많은 시간을 들였지만 마침내 그대로 두어야했습니다. –
이 문제는 이미 여기에서 언급되었습니다. http://stackoverflow.com/a/13470842/1140682 – saschoar