title: How to create nested fragments view in android?
body: <p>I am trying to create a nested fragment view with two fragments in which fragment will contain ListView/RecyclerView, second listview data will be dependent on first listview item.</p>
  
  <p>It will be similar to product filter screen of any e-commerce application.</p>
  
  <p>Please take a look at code below:</p>
  
  <p>TestActivity.java which holds parent fragment:</p>
  
  <pre><code>public class TestActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_test);
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      FilterFragment filterFragment = new FilterFragment();
      ft.replace(R.id.filter_fragment_container, filterFragment);
      ft.commit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>layout file for activity activity_test.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_test"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.splice.TestActivity"&gt;
  &lt;FrameLayout
      android:id="@+id/fragment_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Parent fragment class FilterFragment.java</p>
  
  <pre><code>public class FilterFragment extends Fragment{
  
  
  public FilterFragment() {
      // Required empty public constructor
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View v = inflater.inflate(R.layout.fragment_filter, container, false);
      return v;
  }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      insertNestedFragment();
  }
  
  // Embeds the child fragment dynamically
  private void insertNestedFragment() {
      Fragment childFragment = new TestFragment1();
      FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
      transaction.replace(R.id.fragment1_container, childFragment).commit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Parent fragment layout file fragment_filter.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:layout_margin="@dimen/activity_horizontal_margin"
  tools:context="com.splice.views.FilterFragment"&gt;
  
  &lt;!-- TODO: Update blank fragment layout --&gt;
  &lt;TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center_horizontal"
      android:text="@string/filter_purchase_title" /&gt;
  
  &lt;FrameLayout
  android:layout_width="match_parent"
  android:id="@+id/fragment1_container"
  android:layout_height="wrap_content"&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Child Fragment class TestFragment1.java</p>
  
  <pre><code>public class TestFragment1 extends Fragment {
  
  
  public TestFragment1() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_test_fragment1, container, false);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>In result it is just showing content of Parent Fragment not content of child fragment.</strong></p>
  
  <p>I want to implement somthing like 
  <a href="https://i.stack.imgur.com/5e6Vy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5e6Vy.jpg" alt="enter image description here"></a></p>
  
  <p>Please correct me if I am going for wrong approch to implement this.</p>
  
  <p>Thank you.</p>