我使用的適配器ListView
實現了SectionIndexer
。 ListView
已將fastScrollEnabled
設置爲xml文件中的true。在Android 2.2和2.3上一切正常,但當我使用Android 3.0在平板電腦上測試我的應用程序時,某些部分的滾動條消失了。例如,當我向下滾動列表時,以字母開頭的元素A-B滾動條是可見的,但對於字母C-H,它不是,然後H再次可見。在HoneyComb的特定部分使用SectionIndexer時,滾動條消失
此適配器用於在ListView中按字母順序排序內容,以便可以使用快速滾動。
應用程序是爲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;
}
}
是我存儲與他們的最後出現的索引字母一個HashMap,所以當我開始以字母「A」 6元,價值爲關鍵「 A「是5等等。
alphabet
是一個字符串數組,包含所有現有的第一個字母。
你知道嗎?我正在處理同樣的問題。它狡猾我的問題是與getSectionForPosition方法。 – KickingLettuce
不幸的不是。花了很多時間弄清楚這一點,但最後不得不離開它。 –
此問題已在此處提及:http://stackoverflow.com/a/13470842/1140682 – saschoar