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:min
There is Nested Fragments!!!!!!! in the new support library version(support library v4 , version 11)
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.
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()
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.
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.