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

标签 java android

我正在为实现 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/

相关文章:

android - Android中使用sqlite事务时的多线程问题

java - 无法将 Assets 发送到 Android Wear 设备

java - 如何更改文本字段的值 + jQuery + Struts2

java - 在 Java 中创建自定义文件系统实现

java - 使用 Quartz 和 RabbitMQ 在 Grails 应用程序中写入 NFS 下的文件

android - 当没有可用的互联网连接时应用程序崩溃

java - Jackson JSON 未设置 Map<String, Object> 值

java - JSON、Jersey 和 Jackson 中的多态性

android - 如何在 AndroidX 上使用该库?

android - Android 上的 SurfaceView 无法获得高 fps