Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would change your architecture for this one on the android developer documentation:</p> <p><a href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html" rel="noreferrer">http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html</a></p> <p>but I would change some things...</p> <p>1-I would change this method:</p> <pre><code>/** * The Fragment's UI is just a simple text view showing its * instance number. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_pager_list, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("Fragment #" + mNum); return v; } </code></pre> <p>For something like this where we decide which fragment you populate depending the position of the viewPager:</p> <pre><code>@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); SupportFragmentManager ft = getChildFragmentManager().beginTransaction(); String tag = ""; Fragment fragment = null; switch (mNum) { case 0: fragment = new MyFragmentZero(); tag = FragmentTags.TAG_0; break; case 1: fragment = new MyFragmentOne(); tag = FragmentTags.TAG_3; break; case 2: fragment = new MyFragmentTwo(); tag = FragmentTags.TAG_2; break; default: break; } /*OPTIONAL We can pass arguments to the fragments Bundle args = new Bundle(); args.putInt(Arguments.ARG_POSITION, mNum); fragment.setArguments(args);*/ //Place the fragment in the container ft.replace(R.id.fragment_container fragment, tag); ft.commit(); //You need a base layout for all fragment and use nested fragments later or you can define the layout for each position(mNum) inside the switch. return inflater.inflate(R.layout.fragment_layout_default_for_all_views, container, false); } </code></pre> <p>Like this you will have a good architecture and once it is working like this should be fine. </p> <p>Anyway you must know how the viewPager works populating the fragment in the different positions. </p> <p>When you start on the position 0, then the fragment on the position 0 and the one of the position 1 are created.</p> <p>Then when you swipe to the position 1 the fragment on the 2 position is created, so you have now the three fragments created on the different positions (0,1,2..assuming you have only 3 pages on the viewPager).</p> <p>We swipe to the position 2, the last one, and the fragment on the first position (0) get destroy, so we have now the fragments on the positions 2 and 3.</p> <p>I hope it helped and let me know if you have any problem. Cheers</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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