ListFragment设置初始选择

发布于 2024-12-04 14:03:55 字数 68 浏览 0 评论 0原文

希望是一个快速的问题。我将如何在 ListFragment 上设置默认选择。我希望活动在已选择顶部列表项的情况下启动。谢谢

hopefully a quick question. How would I go about setting the default selection on a ListFragment. I want the activity to launch with the top list item already selected. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2024-12-11 14:03:55

取自 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 ....
}

看一下我在顶部链接到的示例,这应该可以帮助你启动并运行!

Taken from the official example in the Android docs (http://developer.android.com/guide/components/fragments.html#Example) and in the support library API demos:

The ListFragment in that example uses getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); and getListView().setItemChecked(index, true); in the ListFragment's onActivityCreated method to make a list item selected/highlighted, where index is taken from a local variable set to 0 by default. So you'd have something like:

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 ....
}

Take a look at that example I linked to at the top and that should get you up and running!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文