ListView.setSelection不起作用

时间:2014-08-19 22:35:52

标签: java android listview

我有一个带自定义适配器的ListView。当我旋转屏幕时,它总是转到第一个位置。我已经尝试保存最后一个位置,然后在正确的位置使用SetSelection设置位置,但现在没有运气。

一段代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_words, container, false);
    lvIrregularVerbs = (ListView) root.findViewById(R.id.lvSimple);
    if (savedInstanceState != null) {
        lastposition=savedInstanceState.getInt("position");
    }
    fulllist(lst);
    irregularVerbsAdapter = new IrrVerbsAdapter(getActivity(), lst, MainActivity.db);
    lvIrregularVerbs.setAdapter(irregularVerbsAdapter);
    lvIrregularVerbs.setOnItemClickListener(new OnItemClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        //some stuff


        }
    });
    lvIrregularVerbs.setSelection(lastposition);
    return root;    
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("position", lvIrregularVerbs.getLastVisiblePosition());
}

但是如果我在SetSelection中设置一个常量,那么一切正常。 我看了一眼,尝试了一切,不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

尝试在其他地方发布您的选择代码 - 在onResume中或尝试调用新的Handler()。post(new Runnable(){.. run(){list.setPosition(pos);}});