0

Is it possible to replace a fragment in a FrameLayout with the .replace() to use a ViewPager? since both are ViewGroups.

For example, in xml, I have only

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<FrameLayout
    android:id ="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

and I would like to replace the FrameLayout with id content that is holding a fragment to a ViewPager of fragments in runtime. Is it possible??

Iulia Barbu
  • 1,522
  • 12
  • 25
XepterX
  • 1,017
  • 6
  • 16

1 Answers1

4

i would like to replace the framelayout with id content that is holding a fragment to a viewpager of fragments in runtime. is it possible??

You can't replace a Fragment with a simple ViewGroup like a ViewPager, but thanks to the new changes in 4.2 you could put the ViewPager itself in a Fragment and use nested Fragments as the children of the ViewPager. Then you could replace the initial Fragment with the Fragment that holds the ViewPager.

See this guide, nested Fragments are also available to lower APIs through the compatibility package. See this question for an implementation example.

Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190