Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing a fragment from ViewPager android
    primarykey
    data
    text
    <h2>SCENARIO</h2> <p>I am creating a fragment structure with dynamic fragment creations. Each item in the fragment creates the next fragment. In the scenario I am storing all these fragments in the ArrayList of fragments so that I can easily replace the created fragment. </p> <h2>PROBLEM</h2> <p>Now I am replacing a fragment from the ArrayList by removing the fragment from a particular index and adding the new one. But when I try to get the fragment from that particular index it returns me the old data without calling the getItem function of the FragmentStateAdapter again.</p> <p>Here is my Main class.</p> <h2>MainActivity.java</h2> <pre><code>public class MainActivity extends FragmentActivity implements onPageSelectedListener { SectionsPagerAdapter mSectionsPagerAdapter; static int current, listItem; static String TAG = "MainActivity"; static ViewPager mViewPager; static ArrayList&lt;Fragment&gt; FragmentList; static ArrayList&lt;String&gt; titles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ FragmentList = new ArrayList&lt;Fragment&gt;(); titles = new ArrayList&lt;String&gt;(); mViewPager = (ViewPager) findViewById(R.id.pager); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); mViewPager.setAdapter(mSectionsPagerAdapter); } public void addPage(int position, String title, String file, String tag) { Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putString(DummySectionFragment.ARG_FILE_NAME, file); args.putString(DummySectionFragment.ARG_FILE_TAG, tag); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position); fragment.setArguments(args); titles.add(position, title); FragmentList.add(position, fragment); } public void NextScreen(int position, String title, String file, String tagname) { if(FragmentList.size()&gt;(position-1)){ for(int i=position; i&lt;FragmentList.size(); i++){ FragmentList.remove(i); } } addPage(position, title, file, tagname); mViewPager.setCurrentItem(position); } public static class PageChanger extends ViewPager.SimpleOnPageChangeListener { private int currentPage; @Override public void onPageSelected(int position) { currentPage = position; } public int getCurrentScreen() { return currentPage; } } public class SectionsPagerAdapter extends FragmentStatePagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); addPage(0, "Main Fragment", "test.xml", "mytag"); } @Override public Fragment getItem(int position) { return FragmentList.get(position); } @Override public int getCount() { return FragmentList.size(); } @Override public CharSequence getPageTitle(int position) { if (position == 0) { return "Main Categories"; } getItem(position); return titles.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); } } public static class DummySectionFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; public static final String ARG_FILE_NAME = "file_name"; public static final String ARG_FILE_TAG = "tag_name"; public DummySectionFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { savedInstanceState = this.getArguments(); ListView parent_list = new ListView(getActivity()); String file_name = savedInstanceState.getString(ARG_FILE_NAME); String tag_name = savedInstanceState.getString(ARG_FILE_TAG); int current_level = savedInstanceState.getInt(ARG_SECTION_NUMBER); PullParser pull = new PullParser(getActivity()); pull.pullXml(file_name, tag_name); parent_list.setAdapter(new ListAdapter(getActivity(), PullParser.Xml_Tag_Info, current_level)); return parent_list; } } @Override public void onPageSelected(int pageno) { current = pageno; } } </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.
 

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