3
希望有一个快速的问题。我将如何去设置ListFragment上的默认选择。我想要在已经选择顶部列表项目的情况下启动活动。由于ListFragment设置初始选择
希望有一个快速的问题。我将如何去设置ListFragment上的默认选择。我想要在已经选择顶部列表项目的情况下启动活动。由于ListFragment设置初始选择
在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 ....
}
看看这个例子我挂在顶部,应该让你和运行!