title: ANDROID: ViewPager with different Fragments in instantiateItem method
body: <p>I use <code>ViewPager</code> to create fragments that swipe left and now I want insert fragments into each fragment page.</p>
  
  <p><strong>activityMain.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout
      ...
      android:orientation="vertical"&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/myviewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt;
  
      &lt;devlight.io.library.ntb.NavigationTabBar
          android:id="@+id/ntb_horizontal"
          app:ntb_swiped="true"/&gt;
  
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>and this: 
  <strong>tab0_fragment.xml</strong></p>
  
  
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="40dp"
          android:textSize="30sp"
          android:gravity="center"
          android:id="@+id/textView"
          android:layout_centerHorizontal="true"
          android:textColor="@android:color/holo_green_dark"
          android:text="Social\nFragment"/&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:autoLink="web"
          android:textSize="15sp"
          android:layout_marginTop="10dp"
          android:layout_centerHorizontal="true"
          android:text="androidbelieve.com"
          android:textColor="#000"
          android:layout_below="@+id/textView"
          android:textStyle="italic"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I have a <code>ViewPager</code> in <strong>mainActivity.java</strong></p>
  
  <pre><code>final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
  viewPager.setAdapter(new PagerAdapter() {
  ...
  
  ...
  }
  </code></pre>
  
  <p>I try open one screen in viewpager via fragment layout.</p>
  
  <p>I try this:</p>
  
  <pre><code>final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
  viewPager.setAdapter(new PagerAdapter() {
      ...
      public Object instantiateItem(final ViewGroup container, final int position) {
             if (position == 0)
             {
                  final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0, null, false);
                  FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                  fragmentTransaction.replace(R.id.myviewpager,new tab0Fragment()).commit();
                  container.addView(view);
                  return view;
             }
             else if (position == 1)
             ...
  
     } 
  
  ...
  }
  </code></pre>
  
  <p>AND <strong>tab0Fragment.java</strong></p>
  
  <pre><code>public class tab0Fragment extends Fragment {
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_tab0,null);
      }
  }
  </code></pre>
  
  <p>I am noob in android. how can correct this code?</p>
  
  <hr>
  
  <p><strong>Solution:</strong>
  The answers was not my answer but that help me a lot.</p>
  
  <p>According @Mr. Rabbit ans @Amit Upadhyay I change top code to this:</p>
  
  <pre><code>@Override
              public Object instantiateItem(final ViewGroup container, final int position) {
  
                  if (position == 0)
                  {
                      final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0, null, false);
                      MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
                      adapter.addFragment(new tab0Fragment(), getResources().getString(R.string.tab0));
                      container.addView(view);
                      return view;
                  }
                  else if (position == 1)
                  {
  </code></pre>
  
  <p>and also adding MyAdapter class according @Mr. Rabbit's answer.</p>
  
  <p>hope help someone else.</p>