0

Is there any help, resources, example or a tutorial available for putting fragments inside a fragment?

I have two tabs put up using Action Bar Navigation tabs, which essentially are fragments. What I want to do is, put in a ListFragment and a DialogFragment (to display a view with details) in one of those Action Bar Navigation tabs fragment.

Possible?? Please help. Thanks

Here's what I have done till now:

  1. Followed Android's article on implementing navigation tabs on Action Bar and implemented a two tab action bar, this is running fine and I am able to show two different layouts.
  2. In one of these tabs, I want to show a Fragmented view, for this I have created a FragmentList class with custom ArrayAdapter and data items, the data detail layout and class.
  3. I am now stuck on how to display the fragments inside the tab. Following is the code which shows the content of first tab, how can I modify it to initialize the list fragment properly?

.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{

    View viewer = (View) inflater.inflate(R.layout.doodle_list, container,false);
    return viewer;
}
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
kishu27
  • 3,140
  • 2
  • 26
  • 41

2 Answers2

1

Is there any help, resources, example or a tutorial available for putting fragments inside a fragment?

Fragments inside of fragments is not supported.

Fragments inside of fragments is supported on Android 4.2+ and via the Android Support package's backport of fragments. I'd still look to avoid it where possible.

I have two tabs put up using Action Bar Navigation tabs, which essentially are fragments.

You chose to do that. There is nothing about action bar tabs that requires the use of fragments.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    thanks for your answer, I actually followed http://developer.android.com/guide/topics/ui/actionbar.html#Tabs where they say I have to use Fragments. Can you please point me to some other resource where I can learn how not to use Fragments and still be able to do this? Thanks – kishu27 Mar 23 '12 at 19:31
  • 1
    @kishu27: " where they say I have to use Fragments" -- that's not a good bit of documentation, and I will file a bug report on it shortly. "Can you please point me to some other resource where I can learn how not to use Fragments and still be able to do this?" -- ignore the `FragmentTransaction` passed to the `TabListener` callback methods and do something else to update your UI. – CommonsWare Mar 23 '12 at 19:41
  • 2
    @kishu27 As of API 17 4.2 Jelly Bean, you can do nested Fragments. Here is the [link](http://developer.android.com/about/versions/android-4.2.html#NestedFragments) to Android Developers for nested fragments. – Adam Dec 31 '12 at 04:28
  • @CommonsWare there many articles saying about nested fragments .Even in [developer site](http://developer.android.com/about/versions/android-4.2.html#NestedFragments) – edwin May 13 '13 at 06:25
  • @CommonsWare check this out http://rootzwiki.com/topic/36433-nested-fragments-example-for-android-42-api-v17/ – edwin May 13 '13 at 06:27
  • Neverthless, I'm staying away from nested fragments. Many issues. It works okay but not good for a polished finishing on the app – kishu27 Jul 07 '13 at 18:24
0

I solved this problem. You can use Support library and ViewPager. If you don't need swiping by gesture you can disable swiping. So here is some code to improve my solution:

public class TestFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag, container, false);
    final ArrayList<Fragment> list = new ArrayList<Fragment>();

    list.add(new TrFrag());
    list.add(new TrFrag());
    list.add(new TrFrag());

    ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
    pager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    });
    return v;
}

}

P.S.It is ugly code for test, but it improves that it is possible.

Vetalll
  • 3,472
  • 6
  • 24
  • 34