【发布时间】:2011-09-14 02:23:59
【问题描述】:
希望是一个快速的问题。我将如何在 ListFragment 上设置默认选择。我希望在已选择顶部列表项的情况下启动活动。谢谢
【问题讨论】:
标签: android android-3.0-honeycomb android-fragments
希望是一个快速的问题。我将如何在 ListFragment 上设置默认选择。我希望在已选择顶部列表项的情况下启动活动。谢谢
【问题讨论】:
标签: android android-3.0-honeycomb android-fragments
取自 Android 文档 (http://developer.android.com/guide/components/fragments.html#Example) 和支持库 API 演示中的官方示例:
该示例中的 ListFragment 在 ListFragment 的 onActivityCreated 方法中使用 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 和 getListView().setItemChecked(index, true); 来选择/突出显示列表项,其中 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 ....
}
看看我在顶部链接到的那个例子,它应该可以帮助你启动并运行!
【讨论】: