0

現在、Viewpager Fragment 内に 2 つの Fragments を含めようとしています。すべて正常に動作しますが、さらに 2 つの Fragments をスクロールして、2 つの Fragments がある 1 つに戻ると、上の 1 つだけが表示されます (Fragment B は表示されません)。これをもう一度行うと、両方が再び表示されます。onResume-Event は Fragment B で呼び出されますが、表示されていません。レイアウト XML:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <FrameLayout
            android:id="@+id/a_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/android_color_blue_dark"/>
        <FrameLayout
            android:id="@+id/b_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/android_color_green_dark" />
    </LinearLayout>

onCreateView() の親フラグメント コード

    if (v.findViewById(R.id.a_fragment) != null) {
        FragmentManager mgr = getChildFragmentManager();
        Fragment f = mgr.findFragmentByTag(TAG_A_FRAGMENT);
        if (f == null) {
            f = new FragmentA();
        }
        FragmentTransaction transaction = mgr.beginTransaction();
        transaction.replace(R.id.a_fragment, f, TAG_S_FRAGMENT);
        transaction.commit();
    }
    if (v.findViewById(R.id.b_fragment) != null) {

        FragmentManager mgr = getChildFragmentManager();
        Fragment f = mgr.findFragmentByTag(TAG_B_FRAGMENT);
        if (f == null) {
            f = new FragmentBLogin();
        }
        FragmentTransaction transaction = mgr.beginTransaction();
        transaction.replace(R.id.b_fragment, f,
                TAG_B_FRAGMENT);
        transaction.commit();
    }

このErrorを防ぐために、それらが静的であっても、これらの Fragments を動的に追加します。

4

1 に答える 1

0

以下のように修正しました。なぜこのように機能し、他の方法では機能しないのか、今のところ説明がありません。

if (v.findViewById(R.id.a_fragment) != null) {
    FragmentManager mgr = getChildFragmentManager();
    Fragment f = mgr.findFragmentByTag(TAG_A_FRAGMENT);
    if (f == null) {
        f = new FragmentA();
    FragmentTransaction transaction = mgr.beginTransaction();
    transaction.replace(R.id.a_fragment, f, TAG_S_FRAGMENT);
    transaction.commit();
    }
}
if (v.findViewById(R.id.b_fragment) != null) {

    FragmentManager mgr = getChildFragmentManager();
    Fragment f = mgr.findFragmentByTag(TAG_B_FRAGMENT);
    if (f == null) {
        f = new FragmentBLogin();
    FragmentTransaction transaction = mgr.beginTransaction();
    transaction.replace(R.id.b_fragment, f,
            TAG_B_FRAGMENT);
    transaction.commit();
    }
}
于 2013-07-05T14:59:13.000 に答える