title: ViewPager + Fragments - Fragment not shown
body: <p>I am trying to use a viewpager to display several fragments. At the moment I only have a single fragment but it appears that it is never being instantiated.  I tried using pager.setCurrentItem(0) but the getItem method in the adapter is never being called.  Does anyone see why my fragment is not loading in the below code?</p>
  
  <p>Main Activity</p>
  
  <pre><code>public class MainActivity extends SherlockFragmentActivity {
  
  MyPagerAdapter adapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate( savedInstanceState );
      setContentView( R.layout.activity_main );
  
      // Create the FragmentPager Adapter and assign it to the viewpager
      adapter = new MyPagerAdapter( getSupportFragmentManager(), this );
      ViewPager pager = ( ViewPager ) findViewById( R.id.pager );
      pager.setAdapter( adapter );
      pager.setCurrentItem( 0 );
  
      // Setup the tab page indicator
      TabPageIndicator indicator = ( TabPageIndicator ) findViewById( R.id.indicator );
      indicator.setViewPager( pager );
  }
  
  class MyPagerAdapter extends FragmentStatePagerAdapter {
      private SparseArray&lt;Fragment&gt; fragmentPageMap;
  
      public MyPagerAdapter(FragmentManager fm, Activity context) {
          super( fm );
          fragmentPageMap = new SparseArray&lt;Fragment&gt;();
      }
  
      @Override
      public Fragment getItem(int index) {
          switch ( index ) {
          case 0:
              if ( fragmentPageMap.get( index ) != null ) {
                  SherlockListFragment scheduleFragment =  ScheduleListFragment.newInstance();
                  fragmentPageMap.put( index, scheduleFragment );
                  return scheduleFragment;
              } else {
                  return fragmentPageMap.get( index );
              }
          default:
              return null;
          }
      }
  
      /*@Override
      public void destroyItem(View container, int index, Object object) {
          super.destroyItem( container, index, object );
          fragmentPageMap.remove( index );
      }*/
  
      @Override
      public CharSequence getPageTitle(int index) {
          return ( ( ViewPagerPage ) fragmentPageMap.get( index ) ).getTitle();
      }
  
      @Override
      public int getCount() {
          return fragmentPageMap.size();
      }
  }
  
  public interface ViewPagerPage {
      String getTitle();
  }
  </code></pre>
  
  <p>MainActivity Layout</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="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" &gt;
  
  &lt;com.viewpagerindicator.TabPageIndicator
      android:id="@+id/indicator"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textStyle="bold" /&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="fill_parent"
      android:layout_height="0dp"
      android:layout_weight="1" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>ScheduleListFragment </p>
  
  <pre><code>public class ScheduleListFragment extends SherlockListFragment implements ViewPagerPage {
  
  private ScheduleAdapter adapter;
  private final String TITLE = "Schedule";
  
  public static ScheduleListFragment newInstance() {
      ScheduleListFragment newFrag = new ScheduleListFragment();
      Bundle args = new Bundle();
      newFrag.setArguments( args );
      return newFrag;
  }
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return inflater.inflate( R.layout.fragment_schedule_list, null );
  }
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      inflater.inflate( R.menu.fragment_schedule_list, menu );
  }
  
  // Container Activity must implement this interface
  public interface OnAddItemClickedListener {
      public void onAddItemClicked();
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle item selection
      switch ( item.getItemId() ) {
      case R.id.add_item:
          Intent myIntent = new Intent( getActivity(), AddScheduleItemActivity.class );
          startActivityForResult( myIntent, 1 );
  
          return true;
      default:
          return super.onOptionsItemSelected( item );
      }
  }
  
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated( savedInstanceState );
      setHasOptionsMenu( true );
  
      adapter = new ScheduleAdapter( getActivity() );
  
      setListAdapter( adapter );
  }
  
  private class SampleItem {
      public String tag;
      public int iconRes;
  
      public SampleItem(String tag, int iconRes) {
          this.tag = tag;
          this.iconRes = iconRes;
      }
  }
  
  public class ScheduleAdapter extends ArrayAdapter&lt;SampleItem&gt; {
  
      public ScheduleAdapter(Context context) {
          super( context, 0 );
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          if ( convertView == null ) {
              convertView = LayoutInflater.from( getContext() ).inflate( R.layout.results_list_row, null );
          }
          ImageView icon = ( ImageView ) convertView.findViewById( R.id.row_icon );
          icon.setImageResource( getItem( position ).iconRes );
          TextView title = ( TextView ) convertView.findViewById( R.id.row_title );
          title.setText( getItem( position ).tag );
  
          return convertView;
      }
  
  }
  
  @Override
  public String getTitle() {
      return TITLE;
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if ( resultCode == android.app.Activity.RESULT_OK ) {
          String name = ( String ) data.getExtras().get( "TEXT" );
          Toast.makeText( getActivity(), name, Toast.LENGTH_LONG ).show();
      }
  }
  </code></pre>
  
  <p>Fragment Layout</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;ListView
      android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" /&gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Thanks,
  Nathan</p>
  
  <p>Current Solutions:</p>
  
  <p>Changed the adapters constructor to initialize the first fragment and add it to the internal map.  Does anyone see anything wrong with doing this, or a way to improve any of the code posted above? Thanks!</p>
  
  <pre><code>public MyPagerAdapter(FragmentManager fm, Activity context) {
          super( fm );
          fragmentPageMap = new SparseArray&lt;Fragment&gt;();
          SherlockListFragment scheduleFragment = ScheduleListFragment.newInstance();
          fragmentPageMap.put( 0, scheduleFragment );
      }
  </code></pre>