Android adds fragment inside fragment

Hi there (and thanks in advance)

I have an application with a location in the Google Play Store (using PagerTabStrip with 5 extra snippets). In some of these snippets, I will need to display some information about the last data update.

I immediately thought about creating a fragment (LastUpdatedFragment), which I would add (socket) to the desired fragments. At first, and since the last updated date should have been the same for each screen, everything worked (I just added the fragment in the xml of the parent fragments that I need, and inside onCreateView I would put the date in the TextView), but after some changes I now need send a specific date for each of these LastUpdatedFragment instances.

I came up with one way - creating new custom attributes for my fragment, and then I set the date that I wanted. After some reading, I came across a simpler way ( Fragments inside fragments - dynamically adding a fragment using the FragmentManager) - I just need a parent fragment to process the input parameters and pass it to the child fragment.

The problem is that although I do not get errors, I also do not get the child fragment, it just does not appear. I would appreciate it if you could lead me in the right direction.

ScoreBoardFragment.java → Parent Slice

    @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>
+4
1

FrameLayout LinearLayout Placeholder.

+3

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


All Articles