5

i have the following layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

Everyting is workinf fine but i want to replace the ViewPager with a Fragment

getSupportFragmentManager().beginTransaction()
                .replace(R.id.view_pager, new CustomFragment())                 
                .commit();

this is not repacing the ViewPager what shall i do???

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
anfy2002us
  • 683
  • 1
  • 7
  • 15
  • Documentation says - `Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.` So I assume you can only replace `Fragments` and NOT other views. – Varun Aug 28 '13 at 18:18

1 Answers1

6

You could create a Fragment that contains the ViewPager, and then replace that Fragment.

public class ViewPagerContainerFragment extends Fragment {

    private ViewPager mViewPager;

    public ViewPagerContainerFragment() {   }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.view_pager_container_fragment, container, false);          

        // Set up the ViewPager, attaching the adapter and ...
        mViewPager = (ViewPager) root.findViewById(R.id.viewPager);

        // do other stuff...

        return root;
    }
}

view_pager_container_fragment.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rlMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"   
        />
</RelativeLayout>

Your Activitys .xml file:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
        </FrameLayout>

And then replace Fragments like this:

Adding the ViewPagerContainerFragment to the Activitys layout:

getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new ViewPagerContainerFragment())                 
                .commit();

And somewhere later in code: (if you want to - replace the ViewPagerFragment with another Fragment)

getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new CustomFragment())                 
                .commit();

Please make sure that you do not create any memory leaks and set up your Adapter properly.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • If you try this solution be aware of navigation back behaviour. Check out [this question](http://stackoverflow.com/questions/12490963/replacing-viewpager-with-fragment-then-navigating-back). – Miguelos Apr 19 '17 at 11:39