gpt4 book ai didi

java - 在 HoneyComb 的特定部分使用 SectionIndexer 时,滚动条消失

转载 作者:IT老高 更新时间:2023-10-28 23:36:06 26 4
gpt4 key购买 nike

我正在为实现 SectionIndexerListView 使用适配器。 ListView 在 xml 文件中将 fastScrollEnabled 设置为 true。在 Android 2.2 和 2.3 上一切正常,但是当我在装有 Android 3.0 的平板电脑上测试我的应用程序时,滚动条在某些部分消失了。例如,当我向下滚动列表时,以字母 A-B 滚动条开头的元素可见,但对于字母 C-H 则不可见,然后在 H 之后再次可见。

此适配器用于在 ListView 中按字母顺序对内容进行排序,以便可以使用 fastscroll。

应用程序是为 API Level 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 是一个包含所有现有首字母的字符串数组。

最佳答案

我遇到了同样的问题。我只是将我的最低 SDK 版本设为 8,将 traget 设为 16,并编写了以下代码。一切正常。

             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);
}

关于java - 在 HoneyComb 的特定部分使用 SectionIndexer 时,滚动条消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10976549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com