0

I have an activity that holds a ViewPager, set up as the template Swipe ViewPager. I have attached two fragments to that ViewPager via an adapter. I am now trying to open a new fragment but I am having great trouble getting it to work. I don't fully understand this idea of changing fragments with FragmentTransaction (I tried reading the Android docs to no avail) so I pieced the FragmentTransaction code together from places over the web.

This is my activity_main.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>

This is the code I used to try to open the fragment

private void openVolumeSettingsFragment() {
    Activity activity = getActivity();
    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.pager, new VolumeSettingsFragment());
    transaction.addToBackStack(null);
    transaction.commit();
}

This is the blank xml file for my new fragment fragment_volume_settings.xml

<FrameLayout 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"
             tools:context="com.velixo.bitchtalkandroid.fragments.VolumeSettingsFragment">

    <ListView
        android:id="@+id/volume_settings_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

The only thing this does is freezes my current fragment from being swipeable. Does anybody have any idea of what I should be doing?

Velixo
  • 694
  • 6
  • 11

1 Answers1

0

Posted the same question on reddit, x-posting the answer from jeffinmadison in case anyone reading this question will need it:

If you want to replace the pager fragment with your settings fragment you should make the activity_main be a FrameLayout with a @+id/content and within the FrameLayout put the ViewPager with @+id/pager and then call the replaceFragment on the R.id.content

<FrameLayout
    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:id="@+id/content">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"/>    

</FrameLayout>
Velixo
  • 694
  • 6
  • 11