Я использую адаптер для ListView
, который реализует SectionIndexer
. ListView
имеет fastScrollEnabled
значение true в xml файле. Все отлично работает на Android 2.2 и 2.3, но когда я тестирую свое приложение на планшете с Android 3.0, в некоторых разделах прокрутка полосы исчезает. Например, когда я просматриваю список, на элементах, начинающихся с букв A-B, видима полоса прокрутки, но для букв C-H это не так, а затем после того, как H снова видится.
Этот адаптер предназначен для сортировки содержимого в алфавитном порядке в ListView, чтобы можно было использовать fastscroll.
Приложение разработано для уровня API 8, поэтому я не мог использовать fastScrollAlwaysVisible.
Вот код моего адаптера:
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, где я храню буквы с их последним отображаемым индексом, поэтому, когда у меня есть 6 элементов, начинающихся с буквы "A", значение для ключа "A" равно 5 и так далее.
alphabet
- это массив строк со всеми существующими первыми буквами.