How to replace View Pager view completely with a new Fragment in Android
-
I have the following xml file: <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> I have three fragments in the view pager. In one of the fragment I'm trying to launch a new fragment using this code : FragmentTransaction trans = getFragmentManager() .beginTransaction(); trans.replace(R.id.pager, new SubscriptionFragment()); trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); trans.addToBackStack(null); trans.commit(); I'm able to successfully replace the existing fragment of the ViewPager, but it doesn't serve my purpose, what I desire to do is to not replace the existing fragment already present in the viewPager, but to remove the view pager itself from the view and launch a new Fragment so that the top tabs are no longer visible and ViewPager goes of the view. This works when I launch a new activity using intent. but isn't working for a fragment. Edited : public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new MainFragment()).commit(); } } MainFragment: public class MainFragment extends Fragment { FragmentPagerAdapter adapterViewPager; ViewPager viewPager; String title[] = {"Services", "You", "History"}; // TODO: Rename and change types and number of parameters @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /* Inflate the layout for this fragment */ View view = inflater.inflate(R.layout.fragment_view_pager, container, false); /*************************************/ //CALLING HISTORY API // loadSubscriptions(); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ViewPagerAdapter mAdapter = new ViewPagerAdapter(getFragmentManager()); ViewPager mPager = (ViewPager) getView().findViewById(R.id.pager); mPager.setAdapter(mAdapter); } public static class ViewPagerAdapter extends FragmentPagerAdapter { public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int num) { if (num == 0) { return new ServicesFragment(); } else { return new HistoryFragment(); } } @Override public int getCount() { return 2; } } } activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </FrameLayout> fragment_view_pager.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> In servicefragment : I'm openig new fragment using : physiotherapyView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentTransaction trans = getFragmentManager() .beginTransaction(); /* * IMPORTANT: We use the "root frame" defined in * "root_fragment.xml" as the reference to replace fragment */ trans.replace(R.id.pager, new SubscriptionFragment()); /* * IMPORTANT: The following lines allow us to add the fragment * to the stack and return to it later, by pressing back */ trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); trans.addToBackStack(null); trans.commit(); } });
-
Answer:
Do not Add, ViewPager Inside FrameLayout. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </FrameLayout> Add Fragment in this layout, which contains ViewPager and add FragmentTransaction in your mainActivity public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new MainFragment()).commit(); Here, MainFragment contains ViewPager, Now you can replace your viewPager from entire layout with FragmentTransaction
user3458008 at Stack Overflow Visit the source
Related Q & A:
- How do I apply the overlay to draw a line in maps Android?Best solution by Stack Overflow
- How do you pass the last level in a new Super Mario on ds?Best solution by Yahoo! Answers
- How do I set up ATT email on a new computer?Best solution by Yahoo! Answers
- How do I transfer my "favorites" list to a new computer?Best solution by Yahoo! Answers
- How to forward emails from an old to a new email address?Best solution by webapps.stackexchange.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.