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 pager view. In one of the fragments, I am trying to start 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 can successfully replace the existing fragment of the ViewPager, but it does not serve my purpose, I want not to replace the existing fragment already present in the viewPager, but to remove the view player itself from the view and launch a new fragment, so that the top tabs are no longer visible, and ViewPager leaves the view. This works when I start a new action using intention. but does not work for the fragment.
Edited by:
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 open a new fragment using:
physiotherapyView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentTransaction trans = getFragmentManager() .beginTransaction(); trans.replace(R.id.pager, new SubscriptionFragment()); trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); trans.addToBackStack(null); trans.commit(); } });