簡體   English   中英

如何在具有相同ID的ViewPager片段中添加片段,而視圖分頁器中添加的每個片段都相同?

[英]How to add fragment inside a fragment of ViewPager with same id where each fragment added in view pager is same?

我正在嘗試在視圖分頁器中添加的另一個片段中添加一個片段。 視圖分頁器中的所有片段都是相同的。 我只是通過滑動查看它們。 單擊按鈕后,我通過使用添加片段在我們的主要片段中添加了一個片段。 在執行此操作時,此新片段將被添加到視圖分頁器的最后一個片段中,而不是當前片段。 在我看來,原因是當我們尋找片段時,我們仍然有最后一個片段處於活動狀態,並且該片段在其頂部添加了這個新片段。

首先一切都很好

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>

對於嵌套片段,應使用要在其中添加片段的片段的子片段管理器。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM