Skip to content Skip to sidebar Skip to footer

Can We Nested Two Child Fragment Inside One Parent Fragment In View Pager?

I have a view pager,in one of tab I have parent fragment which contained two child fragment. Candidatelist is the parent fragment which had a frame container I replace one child(c

Solution 1:

I have resolved the issued on the second child fragment I used getFragmentManager

CandidateStartEvent childFragment = new CandidateStartEvent();
FragmentTransaction transaction =getFragmentManager().beginTransaction();
transaction.replace(R.id.childContainer, childFragment).addToBackStack(null).commit();

Solution 2:

You can add child fragment in Parent fragment here i am replace fragment using spinner selection. 1) reports_main_segment.xml 2) ReportFragment .java (Parent fragment)

I have two child fragment like

1) LeadsReportFragment 2) OccupancyFragment

1) reports_main_segment.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="match_parent"
              android:background="@color/gini_white_color"
              android:orientation="vertical">

    <Spinner
        android:id="@+id/report_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:background="@drawable/gray_border_edittext"
        android:dropDownVerticalOffset="80dp"
        android:entries="@array/reports_status"
        android:padding="@dimen/_5sdp"
        android:textColor="@color/gini_gray_color_b7b7b7"/>


    <LinearLayout
        android:id="@+id/child_fragment_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></LinearLayout>
</LinearLayout>

2) ReportFragment.java

public class ReportFragment extends Fragment {
    private ViewGroup root;
    private Spinner report_spinner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = (ViewGroup) inflater.inflate(R.layout.reports_main_segment,
                null);
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        FindViewById();
    }


    private void FindViewById() {
        report_spinner = (Spinner) root.findViewById(R.id.report_spinner);
        report_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String selected = adapterView.getItemAtPosition(i).toString();
                if (selected.equalsIgnoreCase("Leads")) {
                    setFragment(1);
                } else if (selected.equalsIgnoreCase("Occupancy")) {
                    setFragment(2);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // ToDo nothing selection
            }
        });

    }

    public void setFragment(int type) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (type == 1) {
            LeadsReportFragment fragment = new LeadsReportFragment();
            fragmentTransaction.replace(R.id.child_fragment_view, fragment);
        } else if (type == 2) {
            OccupancyFragment fragment = new OccupancyFragment();
            fragmentTransaction.replace(R.id.child_fragment_view, fragment);
        }
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

    }


}

Post a Comment for "Can We Nested Two Child Fragment Inside One Parent Fragment In View Pager?"