title: Android: ViewPager, ActionBar and Android.app.Fragments
body: <p>i have an actionbar with two tabs and an tablistener which handles the fragments. Now i want with a ViewPager the possibility to Swipe to also switch the tabs.
  I tried the solution stated here:
  <a href="https://stackoverflow.com/questions/14172173/android-how-to-mix-actionbar-tab-view-pager-listfragment">Android, How to mix ActionBar.Tab + View Pager + ListFragment</a></p>
  
  <p>But it gives an conflicts with Android.app.Fragments and the Support Package Fragments.
  The App is for >4.0 so i dont need the support fragments.</p>
  
  <pre><code>public class MainActivity extends Activity{
  ....
      actionBar = getActionBar();
      actionBar.setDisplayShowTitleEnabled(false);
      actionBar.setDisplayShowHomeEnabled(false);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      //viewpager
      mViewPager = new ViewPager(this);
      setContentView(mViewPager);
  </code></pre>
  
  <p>Doesnt work:</p>
  
  <pre><code>    //TabsAdapter tabsAdapter = new TabsAdapter(this,mViewPager);  
     //        Tab tab1 = actionBar.newTab().setText("Tab1");
     //        Tab tab2 = actionBar.newTab().setText("Tab2");
     //        tabsAdapter.addTab(tab1, Tab1Fragment.class, null);
     //        tabsAdapter.addTab(tab2, Tab2Fragment.class, null);
  </code></pre>
  
  <p>Alternative:</p>
  
  <pre><code>    //viewpager
      mViewPager = new ViewPager(this);
      setContentView(mViewPager);
      mViewPager.setOnPageChangeListener(
              new ViewPager.SimpleOnPageChangeListener() {
                  @Override
                  public void onPageSelected(int position) {
                      // When swiping between pages, select the
                      // corresponding tab.
                      super.onPageSelected(position);
                      getActionBar().setSelectedNavigationItem(position);
                  }
              });
  
  
  
      tab_1 = actionBar.newTab().setText("Tab1");
  
      tab_1
              .setTabListener(new TabListener&lt;Tab1Fragment&gt;(
                      this, "Tab1", Tab1Fragment.class,mViewPager));
  
      actionBar.addTab(tab_1);
  
  
      tab_2 = actionBar.newTab().setText("Tab2");
      tab_2
              .setTabListener(new TabListener&lt;Tab2Fragment&gt;(
                      this, "Tab2", Tab2Fragment.class,mViewPager));
  
      actionBar.addTab(tab_2);
  
  
  
  
   /**
   * TabListener 
   * @param &lt;T&gt;
   */
  private static class TabListener&lt;T extends Fragment&gt; implements ActionBar.TabListener
          {
      private Fragment mFragment;
      private Activity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
      private ViewPager vp;
  
      public TabListener(Activity activity, String tag, Class&lt;T&gt; clz, ViewPager vp) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
          mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
          this.vp = vp;
      }
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
  
          if (mFragment == null) {
              mFragment = Fragment.instantiate(mActivity, mClass.getName());
              ft.replace(android.R.id.content, mFragment, mTag);
          } else {
              if (mFragment.isDetached()) {
                  ft.attach(mFragment);
              }
          }
         vp.setCurrentItem(tab.getPosition());
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  
          if (mFragment != null) {
              ft.detach(mFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
      }
  
  
  }
  </code></pre>
  
  <p>In the Fragments classes i have statements like:</p>
  
  <pre><code>        Bundle args = new Bundle();
          args.putString("name", atn.getName());
          args.putLong("aid", atn.getId());
          AFragment f = new AFragment();
          f.setArguments(args);
          f.show(getFragmentManager(), "tag");
  
      FragmentManager fragmentManager =mActivity.getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      Fragment f = fragmentManager.findFragmentByTag("Tab1");
      if (f != null) {
  
          fragmentTransaction.detach(f);
          fragmentTransaction.attach(f);
      }
      fragmentTransaction.replace(android.R.id.content, f);
      fragmentTransaction. commitAllowingStateLoss();
  </code></pre>
  
  <p>which are not compatible with the other Fragment type (import android.support.v4.app.Fragment;). How can i easily add the swipe gesture? Selection on Tabs works perfectly.</p>
  
  <p>EDIT:
  With support package 13 the view pager worked but the content isnt refreshed. I use FragmentStatePagerAdapter so the Fragments should be removed and added again but instead it takes the same fragment without new creation. Also Viewpage named teh Fragments like
  android:switcher... can i name them on my own?</p>
  
  <pre><code>    import android.support.v13.app.FragmentStatePagerAdapter;
  
  
  public class TabsAdapter extends FragmentStatePagerAdapter
      implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
  private final Context mContext;
  private final ActionBar mActionBar;
  private final ViewPager mViewPager;
  private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;();
  
  private Fragment mFragment;
  private Activity mActivity;
  private String mTag;
  
  
  
  
  private final List&lt;Fragment&gt; fragments = new ArrayList&lt;Fragment&gt;();
  
  static final class TabInfo {
      private final Class&lt;?&gt; clss;
      private final Bundle args;
      private final String name;
  
  
      TabInfo(Class&lt;?&gt; _class, Bundle _args,String name) {
          clss = _class;
          args = _args;
          this.name=name;
  
      }
  }
  
  public TabsAdapter(Activity activity, ViewPager pager) {
      super(activity.getFragmentManager());
      mContext = activity;
      mActionBar = activity.getActionBar();
      mViewPager = pager;
      mViewPager.setAdapter(this);
      mViewPager.setOnPageChangeListener(this);
      mActivity = activity;
  
  }
  
  public void addTab(ActionBar.Tab tab, Class&lt;?&gt; clss, Bundle args) {
      TabInfo info = new TabInfo(clss, args,tab.getText()+"");
      tab.setTag(info.name);
      tab.setTabListener(this);
      mTabs.add(info);
      mActionBar.addTab(tab);
      notifyDataSetChanged();
  }
  
  @Override
  public int getCount() {
      return mTabs.size();
  }
  
  @Override
  public int getItemPosition(Object object) {
  
      return POSITION_NONE;
  //       return super.getItemPosition(object);
  
  
  }
  
  @Override
  public Fragment getItem(int position) {
  
  
  
  TabInfo info = mTabs.get(position);
   Fragment fr = Fragment.instantiate(mContext, info.clss.getName(), info.args);
  //
  //        //addFragment (fr, position);
      return  fr;
  
  }
  
  public void addFragment(Fragment f, int location) {
  
      if (fragments.size() == 0)
          fragments.add(f);
      else
          fragments.add(location, f);
  }
  
  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  }
  
  @Override
  public void onPageSelected(int position) {
      mActionBar.setSelectedNavigationItem(position);
  }
  
  @Override
  public void onPageScrollStateChanged(int state) {
  }
  
  @Override
  public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
      Object tag = tab.getTag();
      for (int i=0; i&lt;mTabs.size(); i++) {
          if (mTabs.get(i).name == tag) {
              updateDataSet(i);
  
              mViewPager.setCurrentItem(i);
          }
      }
  
  }
  
  
  
  
  @Override
  public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
      // TODO Auto-generated method stub
  
  }
  
  
  public void updateDataSet(int pos)
  {
      //Let's update the dataset for the selected genre
      Fragment fragment =
              (mActivity.getFragmentManager().findFragmentByTag(
                      "android:switcher:"+ mViewPager.getId()+":"+pos));
  
      //TabFragment fragment = (TabFragment) getItem(pos);
      if(fragment != null)  // could be null if not instantiated yet
      {
          if(fragment.getView() != null)
          {
              // no need to call if fragment's onDestroyView()
              //has since been called.
            if(fragment instanceof Tab1Fragment){
                ((Tab1Fragment) fragment).refresh();
            }
              else if(fragment instanceof Tab2Fragment){
                ( (Tab2Fragment) fragment).refresh();
            }
          }
      }
  
  }
  </code></pre>