【问题标题】:ListFragment set initial selectionListFragment 设置初始选择
【发布时间】:2011-09-14 02:23:59
【问题描述】:

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

【问题讨论】:

    标签: android android-3.0-honeycomb android-fragments


    【解决方案1】:

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

    看看我在顶部链接到的那个例子,它应该可以帮助你启动并运行!

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2015-12-21
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多