Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 318
jul Avatar asked Mar 16 '12 10:03

jul


People also ask

What are nested fragments?

Fragments represent a behavior or portion of UI in an Activity. Fragments are mostly used in tablets to divide screen and utilize screen space in efficient way. With Android 4.2 nested fragments are introduced, with which now you can embed fragments inside fragments.

How many ways to call Fragment?

There are three ways a fragment and an activity can communicate: Bundle - Activity can construct a fragment and set arguments. Methods - Activity can call methods on a fragment instance. Listener - Fragment can fire listener events on an activity via an interface.

How to add fragments in activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.


2 Answers

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

like image 193
drooooooid Avatar answered Oct 01 '22 21:10

drooooooid


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.

like image 45
Reinier Avatar answered Oct 01 '22 21:10

Reinier