Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To replace a fragment inside a <code>ViewPager</code> you can move source codes of <code>ViewPager</code>, <code>PagerAdapter</code> and <code>FragmentStatePagerAdapter</code> classes into your project and add following code.</p> <p>into <code>ViewPager</code>:</p> <pre><code>public void notifyItemChanged(Object oldItem, Object newItem) { if (mItems != null) { for (ItemInfo itemInfo : mItems) { if (itemInfo.object.equals(oldItem)) { itemInfo.object = newItem; } } } invalidate(); } </code></pre> <p>into FragmentStatePagerAdapter:</p> <pre><code>public void replaceFragmetns(ViewPager container, Fragment oldFragment, Fragment newFragment) { startUpdate(container); // remove old fragment if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } int position = getFragmentPosition(oldFragment); while (mSavedState.size() &lt;= position) { mSavedState.add(null); } mSavedState.set(position, null); mFragments.set(position, null); mCurTransaction.remove(oldFragment); // add new fragment while (mFragments.size() &lt;= position) { mFragments.add(null); } mFragments.set(position, newFragment); mCurTransaction.add(container.getId(), newFragment); finishUpdate(container); // ensure getItem returns newFragemtn after calling handleGetItemInbalidated() handleGetItemInbalidated(container, oldFragment, newFragment); container.notifyItemChanged(oldFragment, newFragment); } protected abstract void handleGetItemInbalidated(View container, Fragment oldFragment, Fragment newFragment); protected abstract int getFragmentPosition(Fragment fragment); </code></pre> <p><code>handleGetItemInvalidated()</code> ensures that after next call of <code>getItem()</code> it return newFragment <code>getFragmentPosition()</code> returns position of the fragment in your adapter.</p> <p>Now, to replace fragments call </p> <pre><code>mAdapter.replaceFragmetns(mViewPager, oldFragment, newFragment); </code></pre> <p>If you interested in an example project ask me for the sources.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload