Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert and remove fragments into viewpager properly
    primarykey
    data
    text
    <p>I'm making an adroid application with viewpager and fragments. I want to make an option to add or remove fragment pages to the pager dynamically. I have a custom FragmentPagerAdapter:</p> <pre><code>public class MyAdapter extends FragmentPagerAdapter implements TitleProvider{ protected final List&lt;PageFragment&gt; fragments; /** * @param fm * @param fragments */ public MyAdapter(FragmentManager fm, List&lt;PageFragment&gt; fragments) { super(fm); this.fragments = fragments; } public void addItem(PageFragment f){ fragments.add(f); notifyDataSetChanged(); } public void addItem(int pos, PageFragment f){ for(int i=0;i&lt;fragments.size();i++){ if(i&gt;=pos){ getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag(getFragmentTag(i))).commit(); } } fragments.add(pos,f); notifyDataSetChanged(); pager.setAdapter(this); pager.setCurrentItem(pos); } public void removeItem(int pos){ getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag(getFragmentTag(pos))).commit(); for(int i=0;i&lt;fragments.size();i++){ if(i&gt;pos){ getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag(getFragmentTag(i))).commit(); } } fragments.remove(pos); notifyDataSetChanged(); pager.setAdapter(this); if(pos&lt;fragments.size()){ pager.setCurrentItem(pos); }else{ pager.setCurrentItem(pos-1); } } @Override public Fragment getItem(int position) { return fragments.get(position).toFragment(); } @Override public int getCount() { return fragments.size(); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } private String getFragmentTag(int pos){ return "android:switcher:"+R.id.pager+":"+pos; } public String getTitle(int position) { String name = fragments.get(position).getName(); if(name.equals("")) return "- "+position+" -"; return name; } } </code></pre> <p>I can remove any fragment except the 0. when I try to remove it I got a NullPointerException on the notifyDataSetChanged(); or on the pager.setAdapter(this); if i comment out the notify. </p> <p>I also got a NullPointerException when I try to insert a new page. When I add the new page to the end of the list it's working fine. I even tried to readd the fragments in the insert with this after the fragments.add(pos,f)</p> <pre><code> for(int i=0;i&lt;fragments.size();i++){ if(i&gt;=pos){ getSupportFragmentManager().beginTransaction().add(fragments.get(i).toFragment(),getFragmentTag(i-1)).commit(); } } </code></pre> <p>if I use <code>getFragmentTag(i-1)</code> I got nullpointer again. With using just <code>i</code> I got illegalstateexception because can not modify fragment's tag. With <code>beginTransaction().add(pager.getId,fragments.get(i).toFragment())</code> it is still nullpointer...</p> <p>My question is: what am I doing wrong, and how can it be done properly? (and maybe: from where <code>notyfyDataSetChanged()</code> get the data when causes nullpointerexception?)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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