在新增的FrameLayout动态Viewpager动态、FrameLayout、Viewpager

2023-09-07 10:12:42 作者:禁止掩饰此心

是否有可能取代片段与 .replace一个的FrameLayout()使用ViewPager?因为两者都是ViewGroups。

例如,在XML中,我只有

 <?XML版本=1.0编码=UTF-8&GT?;<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =FILL_PARENT    机器人:layout_height =FILL_PARENT/><的FrameLayout    机器人:ID =@ + ID /内容    机器人:layout_width =FILL_PARENT    机器人:layout_height =FILL_PARENT/> 

和我想换成与在运行时保持一个片段一个片段ViewPager的标识内容的FrameLayout。是否有可能?

解决方案   

我想更换ID的内容是这样的的FrameLayout  拿着片段在运行时片段的viewpager。是吗  可能吗?

由ViewPager动态适配Item的高度引发的 血案

您不能取代片段用一个简单的的ViewGroup ViewPager ,但由于在4.2的新变化,你可以把 ViewPager 本身在片段并使用嵌套的片段作为的 ViewPager 的孩子。然后,你可以更换初始片段片段保存在 ViewPager

请参阅本指南,嵌套的片段也可通过兼容包下的API。见this问题一个实现的例子。

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??

解决方案

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.