Android:快速滚动ListView getPositionForSection的怪异行为

时间:2018-08-23 16:13:10

标签: java android listview android-arrayadapter listadapter

我正在尝试实现一个具有标题预览功能快速滚动的列表视图。看起来它几乎可以正常工作,但是我遇到了一些奇怪的,类似bug的行为。当我不使用快速滚动条向下滚动时,快速滚动条消失,仅在末尾重新出现。所以似乎有差距或类似的东西。
我的ListView的ArrayAdapter实现了SectionIndexer及其方法getSections()getPositionForSection(int sectionIndex)getSectionForPosition(int position)。我认为getPositionForSection方法会引起麻烦。当我记录sectionIndex给定的值并向下滚动列表时,该值超过了实际节的长度(为20)。此值来自SectionIndexer,而不是我自己。 Android refererence指出:

  

如果该部分的开始位置超出了适配器范围,   该位置必须修剪到不超过适配器大小的范围。

但是当我将值裁剪为0或section_size -1(= 19)时,奇怪的行为不断出现。下面是我的ListView的ArrayAdapter实现SectionIndexer。注意事项:当AsyncTask中的数据发生更改时,将从适配器外部调用updateSections方法。我希望有人知道问题出在哪里!预先感谢。

public class SoortArrayAdapter extends ArrayAdapter<Soort> implements SectionIndexer {
    List<Soort> data;
    private HashMap<String, Integer> alphaIndexer;
    private ArrayList<String> sections;
    public SoortArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, List<Soort> data) {
        super(context, resource, textViewResourceId, data);
        this.data = data;
        sections = new ArrayList<>();
        alphaIndexer = new HashMap<String, Integer>();
    }

    private void updateSections() {
        alphaIndexer.clear();
        sections = new ArrayList<String>();
        for (int i = 0; i < data.size(); i++) {
            String s = data.get(i).getNaam().substring(0, 1).toUpperCase();
            if (!alphaIndexer.containsKey(s)) {
                alphaIndexer.put(s, i);
                sections.add(s);
            }
        }
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        TextView textView = convertView.findViewById(android.R.id.text1);
        textView.setText(data.get(position).getNaam());

        return convertView;
    }

    @Override
    public Object[] getSections() {

        return sections.toArray(new String[0]);
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        System.out.println(sectionIndex);
        if (sectionIndex >= sections.size()) {
            return 0;
        }
        System.out.println("position for section=" + sections.get(sectionIndex));
        return alphaIndexer.get(sections.get(sectionIndex));
    }

    @Override
    public int getSectionForPosition(int position) {
        String section = data.get(position).getNaam().substring(0, 1).toUpperCase();
        System.out.println("section for position=" + section);
        return alphaIndexer.get(section);
    }
}

1 个答案:

答案 0 :(得分:0)

好像我误读了上述方法getSectionForPosition()和getPositionForSection()的文档,尤其是对于getSectionForPosition()。这使getPositionForSection()行为异常。我更正了我的实现,所以这两种方法的最终实现如下:

    @Override
    public int getPositionForSection(int sectionIndex) {
        if (sectionIndex >= sections.size()) {
            return data.size() - 1;
        }

        if (sectionIndex < 0) {
            return 0;
        }

        return alphaIndexer.get(sections.get(sectionIndex));
    }

    @Override
    public int getSectionForPosition(int position) {
        String section = data.get(position).getNaam().substring(0, 1).toUpperCase();
        for (int i = 0; i < sections.size(); i++) {
            if (section.equals(sections.get(i))) {
                return i;
            }
        }
        return 0;
    }
相关问题