Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I used to get around the problem with fragments being tagged when instantiated and the fact that previously tagged fragments cannot change position within the ViewPager when trying to add or remove pages, i.e. getting the exception:</p> <blockquote> <p>java.lang.IllegalStateException: Can't change tag of fragment MyFragment{4055f558 id=0x7f050034 android:switcher:2131034164:3}: was android:switcher:2131034164:3 now android:switcher:2131034164:4</p> </blockquote> <p>This adapter is created with a list of all pages upfront and you can then use setEnabled(int position, boolean enabled) to disable or enable certain pages which hides or shows these pages in the ViewPager.</p> <p>It works by maintaining an internal list of all fragments but exposing to the ViewPager and mapping the positions of enabled fragments only.</p> <pre><code>public class DynamicFragmentPagerAdapter extends FragmentPagerAdapter { public final ArrayList&lt;Fragment&gt; screens = new ArrayList&lt;Fragment&gt;(); private Context context; private List&lt;AtomicBoolean&gt; flags = new ArrayList&lt;AtomicBoolean&gt;(); public DynamicFragmentPagerAdapter(FragmentManager fm, Context context, List&lt;Class&lt;?&gt;&gt; screens) { super(fm); this.context = context; for(Class&lt;?&gt; screen : screens) addScreen(screen, null); notifyDataSetChanged(); } public DynamicFragmentPagerAdapter(FragmentManager fm, Context context, Map&lt;Class&lt;?&gt;, Bundle&gt; screens) { super(fm); this.context = context; for(Class&lt;?&gt; screen : screens.keySet()) addScreen(screen, screens.get(screen)); notifyDataSetChanged(); } private void addScreen(Class&lt;?&gt; clazz, Bundle args) { screens.add(Fragment.instantiate(context, clazz.getName(), args)); flags.add(new AtomicBoolean(true)); } public boolean isEnabled(int position) { return flags.get(position).get(); } public void setEnabled(int position, boolean enabled) { AtomicBoolean flag = flags.get(position); if(flag.get() != enabled) { flag.set(enabled); notifyDataSetChanged(); } } @Override public int getCount() { int n = 0; for(AtomicBoolean flag : flags) { if(flag.get()) n++; } return n; } @Override public Fragment getItem(int position) { return screens.get(position); } @Override public int getItemPosition(Object object) { return POSITION_NONE; // To make notifyDataSetChanged() do something } @Override public void notifyDataSetChanged() { try { super.notifyDataSetChanged(); } catch (Exception e) { Log.w("", e); } } private List&lt;Fragment&gt; getEnabledScreens() { List&lt;Fragment&gt; res = new ArrayList&lt;Fragment&gt;(); for(int n = 0; n &lt; screens.size(); n++) { if(flags.get(n).get()) res.add(screens.get(n)); } return res; } @Override public Object instantiateItem(ViewGroup container, int position) { // We map the requested position to the position as per our screens array list Fragment fragment = getEnabledScreens().get(position); int internalPosition = screens.indexOf(fragment); return super.instantiateItem(container, internalPosition); } @Override public CharSequence getPageTitle(int position) { Fragment fragment = getEnabledScreens().get(position); if(fragment instanceof TitledFragment) return ((TitledFragment)fragment).getTitle(context); return super.getPageTitle(position); } public static interface TitledFragment { public String getTitle(Context context); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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