首页 文章

片段布局没有完全膨胀FragmentTransaction.add / replace

提问于
浏览
0

我试图替换tabLayout片段内的嵌套片段 .

我的app结构遵循这种模式 .

enter image description here

这是Tab Fragment 2的XML

<FrameLayout
    android:id="@+id/scene_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/shows_fragment_list"
        android:tag="list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@+id/shows_fragment_details"
        android:tag="details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@+id/shows_fragment_info"
        android:tag="info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

我正在使用此代码将NestedFragment添加到TabFragment FrameLayout

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View root = inflater.inflate(R.layout.shows_fragment_layout, container, false);

    ShowDetailsFragment newNestedFragment = new ShowDetailsFragment();

    android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction
            .add(R.id.show_fragment_details, newNestedFragment)
            .addToBackStack("null")
            .commit();

    return root;
}

但是当我将嵌套片段添加到Tab片段时,只显示我布局的最后一部分,在这种情况下它是一个列表视图(以红色突出显示),但它似乎总是在最后一个元素声明时布局由相应的Java文件夸大 .

enter image description here

当我把这些布局夸大为自己的活动时,没有通胀问题,所以它必须与我的碎片实现,我出错的任何想法有关?

2 回答

  • 0

    一个好主意总是使用Android设备监视器来调试您的布局 .

    我还会在你的片段的onCreateView上添加一些日志记录 .

  • 0

    好吧我的问题是,在我的ShowDetailsFragment(嵌套片段)的XML布局中,我有一个FrameLayout作为根,但有两个单独的RelativeLayouts作为子节点,意味着只显示最后一个元素,问题通过嵌套解决这两个相对布局在另一个相对布局中 .

    道具指向ctarabusi指向正确的方向,虽然我仍然不确定为什么这只会在作为片段而不是作为活动膨胀时引起问题 .

相关问题