jul
jul

Reputation: 37474

Is there any alternative to nested Fragments?

I've read around that nesting Fragments should be avoided (eg. here), but I can't see how to do the following:

I'm working on a tab application (android:minSdkVersion="12") with the following UI:

enter image description here

The search bar is always here and the user can navigate through several menu options ('home', 'gallery'...). My idea was to use a BaseActivity with a layout containing the search bar and a FrameLayout in which I would load the Fragment corresponding to the user navigation choice.

My issue is that in the 'Home' Fragment I have several tabs, which I wanted to implement the same way, i.e. with a layout containing the tab bar and a FrameLayout in which I would load the corresponding Fragment, and this leads to nested Fragment...

I know that instead of the BaseActivity I could use several activities and include the search bar in every layout, but it would make it appear and disappear every time the user would change activities...

EDIT

I also need a fixed footer, so I cannot use action bar as proposed by CommonsWare in his answer.

Anybody could help?

Upvotes: 6

Views: 7104

Answers (5)

Jay Parker
Jay Parker

Reputation: 641

Good news!!!!! android support library version 11 is already support nested fragment. Support Package, revision 11 (November 2012)

Changes for v4 support library:
    User Interface
        Added support for nested Fragment classes.

How to implement nested fragment, from android developer

You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

There are some key here: getChildFragmentManager() and getParentFragment()

Upvotes: 0

drooooooid
drooooooid

Reputation: 1584

There is Nested Fragments!!!!!!! in the new support library version(support library v4 , version 11)

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006964

My idea was to use a BaseActivity with a layout containing the search bar and a FrameLayout in which I would load the Fragment corresponding to the user navigation choice.

If you use separate activities, rather than trying to force everything into a single activity, you will not wind up with nested fragments.

Upvotes: 0

Reinier
Reinier

Reputation: 3876

You can use ViewPager and the FragmentPagerAdapter for this. ViewPager allows users to swipe between views or (in your case) Fragments. To show tablike-controls, use ViewPagerIndicator.

Using the layout you described, instead of loading a Fragment into the FrameLayout, inflate it with a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/viewpagerIndicator"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

Next, assign a FragmentPagerAdapter to your ViewPager, which will then load your Fragments.

Also take a look at my answer here. It gives a more detailed example. Note however that it extends PagerAdapter, instead of the FragmentPagerAdapter you should be using.

Upvotes: 2

pjanecze
pjanecze

Reputation: 3175

Can't you do tab bars in BaseActivity layout? Then in BaseActivity layout will be search bar, tab bar and fragment frame. If you don't want nav bar in some fragment you can set visibility View.GONE to nav bar.

Upvotes: 0

Related Questions