希望是一个快速的问题。我将如何在ListFragment上设置默认选择。我希望活动在已经选择的顶部列表项的情况下启动。感谢
答案 0 :(得分:1)
取自Android文档(http://developer.android.com/guide/components/fragments.html#Example)和支持库API演示中的官方示例:
该示例中的ListFragment使用ListFragment的getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
方法中的getListView().setItemChecked(index, true);
和onActivityCreated
来选择/突出显示列表项,其中index
取自局部变量默认设置为0。所以你有类似的东西:
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
// (Replace this with your own list adapter stuff
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(mCurCheckPosition, true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
// ... the rest of the ListFragment code ....
}
看一下我在顶部链接的那个例子,这应该可以让你开始运行!