Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace Fragment inside a ViewPager
    primarykey
    data
    text
    <p>I'm trying to use Fragment with a <code>ViewPager</code> using the <code>FragmentPagerAdapter</code>. What I'm looking for to achieve is to replace a fragment, positioned on the first page of the <code>ViewPager</code>, with another one.</p> <p>The pager is composed of two pages. The first one is the <code>FirstPagerFragment</code>, the second one is the <code>SecondPagerFragment</code>. Clicking on a button of the first page. I'd like to replace the <code>FirstPagerFragment</code> with the NextFragment.</p> <p>There is my code below.</p> <pre><code>public class FragmentPagerActivity extends FragmentActivity { static final int NUM_ITEMS = 2; MyAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter); } /** * Pager Adapter */ public static class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { if(position == 0) { return FirstPageFragment.newInstance(); } else { return SecondPageFragment.newInstance(); } } } /** * Second Page FRAGMENT */ public static class SecondPageFragment extends Fragment { public static SecondPageFragment newInstance() { SecondPageFragment f = new SecondPageFragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Log.d("DEBUG", "onCreateView"); return inflater.inflate(R.layout.second, container, false); } } /** * FIRST PAGE FRAGMENT */ public static class FirstPageFragment extends Fragment { Button button; public static FirstPageFragment newInstance() { FirstPageFragment f = new FirstPageFragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Log.d("DEBUG", "onCreateView"); View root = inflater.inflate(R.layout.first, container, false); button = (Button) root.findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentTransaction trans = getFragmentManager().beginTransaction(); trans.replace(R.id.first_fragment_root_id, NextFragment.newInstance()); trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); trans.addToBackStack(null); trans.commit(); } }); return root; } /** * Next Page FRAGMENT in the First Page */ public static class NextFragment extends Fragment { public static NextFragment newInstance() { NextFragment f = new NextFragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Log.d("DEBUG", "onCreateView"); return inflater.inflate(R.layout.next, container, false); } } } </code></pre> <p><strong>...and here the xml files</strong></p> <p><code>fragment_pager.xml</code></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"&gt; &lt;/android.support.v4.view.ViewPager&gt; &lt;/LinearLayout&gt; </code></pre> <p><code>first.xml</code></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/first_fragment_root_id" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="to next"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Now the problem... which ID should I use in</p> <pre><code>trans.replace(R.id.first_fragment_root_id, NextFragment.newInstance()); </code></pre> <p>?</p> <p>If I use <code>R.id.first_fragment_root_id</code>, the replacement works, but Hierarchy Viewer shows a strange behavior, as below.</p> <p>At the beginning the situation is <img src="https://i.stack.imgur.com/29u9S.png" alt=""></p> <p>after the replacement the situation is <img src="https://i.stack.imgur.com/6iMYc.png" alt=""></p> <p>As you can see there is something wrong, I expect to find the same state shown as in the first picture after I replace the fragment.</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.
 

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