문제

I am creating a 'dynamic' ui with fragments, very similar to the example in Android's Building a Dynamic UI guide. The difference being I am wrapping two fragments within a fragment rather than the activity.

The layout-large layout file for the right pane looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_message_multi_pane"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/some_color"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/left_pane"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.rperryng.basicfragments.leftpane"

    <fragment
        android:id="@+id/right_pane"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:name="com.rperryng.basicfragments.rightpane"

</LinearLayout>

the onAttach() method for the leftpane tries to call mListener = (someInterface) getParentFragment(). However, when the layout is inflated with the large layout (i.e. on tablets), the getParentFragment() call is null (confirmed with an if statement and confirmation log message). What gives? Is this due to the system not using the childFragmentManager when inflating directly from a layout or something? do I have to add the fragments at runtime instead? Worth noting I am using the support library

도움이 되었습니까?

해결책

The Android Support Library also now supports nested fragments, so you
can implement nested fragment designs on Android 1.6 and higher.

Note: You cannot inflate a layout into a fragment when that layout includes a 
<fragment>. Nested fragments are only supported when added to a fragment dynamically.

as talked about here http://developer.android.com/about/versions/android-4.2.html#NestedFragments

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top