Android adding Fragment inside Fragment

Bernardo picture Bernardo · Jan 6, 2015 · Viewed 19.9k times · Source

Hi there (and thanks in advance),

I have an application with a Google Play Store-like layout (using PagerTabStrip with 5 sub fragments). In some of those fragments I will need to display a little info regarding the last time the data was updated.

I immediately thought of creating a fragment (LastUpdatedFragment) which I would then add (nest) to the fragments I needed. At first, and since the last updated date was supposed to be the same for every screen, things were working (I simply added the fragment in the xml of the parent Fragments I needed and inside onCreateView I would put the date in the TextView), but after some changes I now need to send a specific date for each one of these LastUpdatedFragment instances.

I came up with one way - creating new custom attributes for my Fragment and then I would set the date I wanted. After some reading I stumbled across a easier way (Fragments within Fragments - dinamically adding the fragment using FragmentManager) - I simply needed the parent Fragment to handle the input parameters and pass it to the child fragment.

The problem is that, although I get no errors I also get no child fragment, it just does not display. I would be grateful if you could guide me in the right direction.

ScoreBoardFragment.java -> Parent Fragment

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

        rootView = inflater.inflate(R.layout.fragment_scoreboard, container,
                false);

        for (int i = 0; i < tvCount; i++) {
            tvName = "f_scoreboard_header_tv" + String.valueOf(i);
            resID = getResources().getIdentifier(tvName, "id",
                    _activity.getPackageName());
            header = (TextView) rootView.findViewById(resID);
            header.setTypeface(MyGlobalConfig.getInstance().getHeadersFont());
        }

        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        LastUpdatedFragment fragB = new LastUpdatedFragment();
        childFragTrans.add(R.id.FRAGMENT_PLACEHOLDER, fragB);
        childFragTrans.addToBackStack("B");
        childFragTrans.commit();        


        return rootView;
    }

fragment_scoreboard.xml (simplified but displaying everything else)

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

        <LinearLayout
            style="@style/ListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/f_scoreboard_header_tv0"
                style="@style/ListViewRowHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="#" />

        </LinearLayout>

        <ListView
            android:id="@+id/f_scoreboard_lvbody"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true" >
        </ListView>

    </LinearLayout>

LastUpdatedFragment.java -> Child Fragment

        public class LastUpdatedFragment extends Fragment{
        private View rootView;
        private TextView tv;
        private Context ctx;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
                    getActivity()));
            this.ctx = getActivity().getApplicationContext();

        }

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

            rootView = inflater.inflate(R.layout.fragment_date, container,
                    false);
            tv = (TextView) rootView.findViewById(R.id.f_date_tv);
            tv.setTypeface(MyGlobalConfig.getInstance().getBodyFont());
            tv.setText(getString(R.string.lastupdated) + ": " + Util.getLastUpdated(ctx));
            return rootView;
        }
    }

fragment_date.xml

    <?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="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/f_date_tv"
            style="@style/LastUpdatedTV"
            android:layout_width="match_parent"
            android:text="Data de última actualização: 27/12/2014 21:30" />
    </LinearLayout>

Answer

Krish picture Krish · Jan 6, 2015

Try to use FrameLayout instead of LinearLayout as Placeholder.