I'm using adapter for ListView
that implements SectionIndexer
. ListView
has fastScrollEnabled
set to true in xml file. Everything works great on Android 2.2 and 2.3, but when I test my application on a tablet with Android 3.0, at some sections scrollbar disappears. For example when I scroll down the list, at elements beginning with letters A-B scrollbar is visible, but for letters C-H it's not, and then after H again visible.
This adapter is made for sorting content alphabetically in ListView so that fastscroll can be used.
Application is designed for API Level 8, so I couldn't use fastScrollAlwaysVisible.
Here is a code of my adapter:
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
is a HashMap where I store letters with their last appearing index so when I have 6 elements starting with letter "A", the value for key "A" is 5 and so on.
alphabet
is a String array with all existing first letters.
I was facing same problem. I just gave my minimum SDK version as 8 and traget as 16 and wrote the code below. And everything worked fine.
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion <= android.os.Build.VERSION_CODES.FROYO){
// Do something for froyo and above versions
fblist.setFastScrollEnabled(true);
} else if(currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){
// do something for phones running an SDK before froyo
fblist.setFastScrollEnabled(true);
fblist.setFastScrollAlwaysVisible(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With