Dynamically load next fragment with ViewPager
<p>I'm creating a demo app with the new android <a href="http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.html">compatibility library</a>. I want to show two listfragments with ViewPager. The first shows the root categories and the second shows the subcategories. After somebody clicks on root category the viewpager move to the subcategory, but it won't work, because always show the root category elements.
I spend few minutes with debug and I'm realised the ViewPager creates the second fragment after the first one.(I think the pager cache the next view or fragment.)</p>

<p>Is it possible to load the second fragment after onListItemClick?</p>

<p>Here is the code:</p>

<pre><code>public class ViewPagerDemoActivity extends FragmentActivity implements OnBookSelectedListener {

private static int mSelectedCategoryId;
private MyAdapter mMyAdapter;
private ViewPager mPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setupPager();
}

private void setupPager() {
    mPager = (ViewPager) findViewById(R.id.pager);
    mMyAdapter = new MyAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyAdapter);
}

@Override
public void onBookSelected(int categoryId) {
    mSelectedCategoryId = categoryId;
    if (mSelectedCategoryId == 0) {
        mPager.setCurrentItem(1);
    } else {
        mPager.setCurrentItem(0);
    }
}

public static class MyAdapter extends FragmentPagerAdapter {
    private static final int NUMBER_OF_FRAGMENTS = 2;

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return BookListFragment.newInstance(mSelectedCategoryId);
    }

    @Override
    public int getCount() {
        return NUMBER_OF_FRAGMENTS;
    }
}
</code></pre>

<p>}</p>

<pre><code>public class BookListFragment extends ListFragment {
private static final String ARGUMENT_KEY = "categoryId";
private OnBookSelectedListener mOnBookSelectedListener;
private int mCategoryId;

public static BookListFragment newInstance(int categoryId) {
    BookListFragment bookListFragment = new BookListFragment();
    Bundle argument = new Bundle();
    argument.putInt(ARGUMENT_KEY, categoryId);
    bookListFragment.setArguments(argument);
    return bookListFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCategoryId = getArguments() != null ? getArguments().getInt(ARGUMENT_KEY) : 0;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, Books.getBooks(mCategoryId)));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.pager_list, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView) tv).setText("Category :" + mCategoryId);
    return v;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (mCategoryId == 0) {
        mOnBookSelectedListener.onBookSelected(1);
    } else {
        mOnBookSelectedListener.onBookSelected(0);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mOnBookSelectedListener = (OnBookSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + "Must Implement OnBookSelectedListener.");
    }
}

public interface OnBookSelectedListener {
    public void onBookSelected(int categoryId);
}
</code></pre>

<p>}</p>

<pre><code>public class Books {
private static final String[] fruits = new String[] { "Apple", "Banana", "Orange" };
private static final String[] category = new String[] { "Lorem", "Dolor", "Ipsum" };

public static String[] getBooks(int categoryId) {
    return categoryId == 0 ? category : fruits;
}
</code></pre>

<p>}</p>
