Search code examples
androidactionbarsherlockfragmentandroid-listfragment

Return different Fragment Types


I have the following method which gives back different Fragments:

@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;
    }

How can I return 2 times a SherlockFragment and 1 time a SherlockListFragment?


Solution

  • Change it this way

        @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;
        }