title: How to add dynamically several fragments
body: <p>I create a little app with 3 static fragments and a view pager adapter. But what I want is to create Only one kind of fragment and with a button "add" , add other fragment which  will be adapted by the view pager.. here is what i have done</p>
  
  <p>FragmentTab1.java ; FragmentTab2 ; FragmentTab3 have the same code</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class FragmentTab2 extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // Get the view from fragmenttab2.xml
          View view = inflater.inflate(R.layout.fragmenttab2, container, false);
          return view;
      }
  
  }
  </code></pre>
  
  <p>here is my MainActivity.java</p>
  
  <pre><code>import android.app.Fragment;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.view.ViewPager;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  
  public class MainActivity extends FragmentActivity {
  
      //Button adfg = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Get the view from activity_main.xml
          setContentView(R.layout.activity_main);        
          // Locate the viewpager in activity_main.xml
          ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
  
          // Set the ViewPagerAdapter into ViewPager
          viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
      }   
  }
  </code></pre>
  
  <p>ViewPagerAdapter.java</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import android.content.Context;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  
  public class ViewPagerAdapter extends FragmentPagerAdapter {
  
      final int PAGE_COUNT = 3;
      // Tab Titles
      private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
  
      Context context;
  
      public ViewPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public int getCount() {
          return PAGE_COUNT;
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
  
              // Open FragmentTab1.java
          case 0:
              FragmentTab1 fragmenttab1 = new FragmentTab1();
              return fragmenttab1;
  
  
              // Open FragmentTab2.java
          case 1:
              FragmentTab2 fragmenttab2 = new FragmentTab2();
              return fragmenttab2;
  
              // Open FragmentTab3.java
          case 2:
              FragmentTab3 fragmenttab3 = new FragmentTab3();
              return fragmenttab3;
          }
          return null;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return tabtitles[position];
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;android.support.v4.view.PagerTabStrip
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="top"
          android:paddingBottom="10dp"
          android:paddingTop="10dp"
          android:textColor="#000000" /&gt;
  
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p>fragmenttab1.xml ; fragmenttab2.xml ; fragmenttab3.xml has the same code</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Fragment 1" /&gt;
  
      &lt;Button
          android:id="@+id/bajou"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Ajouter fragment" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>