Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, so first thing you need to set <strong>OnPageChangeListener</strong> on the ViewPager and implement method <em>onPageSelected(int i)</em> and call the adapter's <em>notifyDataSetChanged()</em>, like so:</p> <pre><code> mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { //Tell the adapter that the content was changed mPager.getAdapter().notifyDataSetChanged(); } @Override public void onPageScrollStateChanged(int i) { } }); </code></pre> <p>In order to keep the fragments updated, you need to extends <strong>FragmentStatePagerAdapter</strong> and not <strong>FragmentPagerAdapter</strong> like what you did. The difference is that with <strong>FragmentPagerAdapter</strong> the <strong>ViewPager</strong> will never re-create the fragments, while in <strong>FragmentStatePagerAdapter</strong> it will.</p> <p>Then on <em>getItem(..)</em> make sure to return a new instance of the fragment with the new content by passing the content to its arguments via <em>setArguments()</em>. Then override also <em>getItemPosition(..)</em> to tell the adapter that the fragment is not found, and therefore it must re-create it.</p> <pre><code>public class MyPagerAdapter extends FragmentStatePagerAdapter { //List to hold the fragments to be shown //NOTE: It's a list of Fragment classes, not a list of Fragment instances! private List&lt;Class&lt;? extends Fragment&gt; fragments; public MyPagerAdapter(FragmentManager fm) { super(fm); fragments.add(SomeFragment.class); fragments.add(AnotherFragment.class); fragments.add(MoreFragment.class); } @Override public Fragment getItem(int i) { try { //Creates a new instance of the fragment Fragment instance = fragments.get(i).newInstance(); //Put the new content by passing Bundle with new content instance.setArguments(args); return instance; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Override public int getItemPosition(Object object) { //NOTE: you might want to put better logic here return POSITION_NONE; } @Override public int getCount() { return pages.size(); } } </code></pre> <p>Every time you slide from one fragment to another, <em>onPageSelected()</em> will be fired calling notifyDataSetChanged() which will force the adapter to check also if the position of the fragment has changed. Since we return POSITION_NONE in <em>getItemPosition(..)</em>, the adapter thinks that the position changed and will then call <em>getItem(i)</em>. In <em>getItem(i)</em> we return a new instance (optionally, passing new arguments). Problem solved! :)</p> <p>I just tested it by myself, created a small app that have a counter which increases everytime the user slides the page and it works!</p> <p>This way you can drop the <em>ViewPager.SimpleOnPageChangeListener.onPageSelected</em>. <a href="http://developer.android.com/training/animation/screen-slide.html" rel="nofollow">Learn more</a> about ViewPager.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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