Is there an alternative to nested fragments?

I read that nested fragments should be avoided (e.g. here ), but I don't see how to do the following:

I am working on a tab application ( android:minSdkVersion="12" ) with the following interface:

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 BaseActivity with a layout containing a search string and FrameLayout, in which I would load the Snippet that matches the navigation choice for the user.

My problem is that in the "home" fragment I have several tabs that I would like to implement in the same way, that is, with a layout containing the tab bar and FrameLayout, in which I would load the corresponding fragment, and this results for the nested fragment ...

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

EDIT

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

Can anybody help?

+6
source share
5 answers

There are nested fragments !!!!!!! in the new version of the support library (support for the v4 library, version 11)

+3
source

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

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

 <?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> 

Then assign the FragmentPagerAdapter your ViewPager, which will then load your snippets.

Also consider my answer here . This gives a more detailed example. Note, however, that it extends the PagerAdapter instead of the FragmentPagerAdapter , which you should use.

+2
source

Can you make tabs in the BaseActivity layout? Then, in the BaseActivity layout, the search bar, the tab bar and the fragment frame will be displayed. If you do not want the navigation bar in some fragment, you can set the visibility of View.GONE to the navigation bar.

0
source

My idea was to use BaseActivity with a layout containing a search bar and FrameLayout, in which I would load the Snippet that matches the navigation choice for the user.

If you use separate actions, rather than trying to force everything into one action, you do not end with nested fragments.

0
source

Good news!!!!! Android version 11 support library already supports nested fragment. Support Package Version 11 (November 2012)

Changes in the v4 support library:
User interface

How to implement a nested fragment from an Android developer

Now you can insert fragments inside fragments. This is useful for various situations in which you want to place dynamic and reusable user interface components in a user interface component that is itself dynamic and can be reused. For example, if you use ViewPager to create fragments that scroll left and right and consume most of the screen space, you can now insert fragments on each page of the fragment.

There are several keywords here: getChildFragmentManager () and getParentFragment ()

0
source

Source: https://habr.com/ru/post/910895/


All Articles