【问题标题】:How to add fragment inside a fragment of ViewPager with same id where each fragment added in view pager is same?如何在具有相同 id 的 ViewPager 片段中添加片段,其中在视图寻呼机中添加的每个片段都相同?
【发布时间】:2015-08-11 15:00:32
【问题描述】:

我正在尝试在视图寻呼机中添加的另一个片段中添加一个片段。视图寻呼机内的所有片段都是相同的。我只是通过滑动查看它们。单击按钮时,我使用添加片段在我们的主片段中添加一个片段。在这样做时,这个新片段被添加到视图寻呼机的最后一个片段而不是当前片段。 在我看来,原因是当我们寻找片段时,我们仍然有最后一个片段处于活动状态,并且该片段在其上添加了这个新片段。

第一次一切正常

public void onAddFragmentClick(View view) {
    if (!isInfoShown) {
        FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();

        fragment = new MainFragment();
        fragmentTransaction.replace(R.id.fragment, fragment, "detail fragment");
        fragmentTransaction.commit();
        isInfoShown = true;
    } else {
        FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();

        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
        isInfoShown = false;
    }
}

在viewpager中添加的主要片段是

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

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.wedm.wedme.ui.fragment.PictureScreenFragment"
    android:id="@+id/fragment"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal|top" />
</FrameLayout>

</RelativeLayout>

【问题讨论】:

  • 对于嵌套片段,您应该使用要添加片段的片段的子片段管理器。希望这会有所帮助。
  • 谢谢伙计。你拯救了我的一天。请在答案中添加此内容。

标签: android android-fragments android-viewpager fragment


【解决方案1】:

对于嵌套片段,您应该使用要添加片段的片段的子片段管理器。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多