返回不同的片段类型

时间:2013-06-07 12:58:39

标签: android actionbarsherlock fragment android-listfragment

我有以下方法可以返回不同的碎片:

@Override
    public SherlockFragment getItem(int position) {
        SherlockFragment fragment = null;
        if (position == 0) {
            fragment = new MyFragment();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        } else if (position == 1) {
            fragment = new MyFragment2();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        } else if (position == 2) {
            fragment = new MyListFragment();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        }
        return fragment;
    }

如何将SherlockFragment返回2次,将SherlockListFragment返回1次?

1 个答案:

答案 0 :(得分:2)

以这种方式改变

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if (position == 0) {
            fragment = new MyFragment();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        } else if (position == 1) {
            fragment = new MyFragment2();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        } else if (position == 2) {
            fragment = new MyListFragment();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        }
        return fragment;
    }