Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-listfragment

How can i use fragment xml tag in layout for another fragment?


i'm trying to do a three page which contains list fragment in an activity. All of the pages are fragment(from v4 support library) but i need list fragment inside them. When i tried that i have encountered a problem(https://stackoverflow.com/questions/29473626/viewpager-gives-error-if-i-turn-page) then i have done some research and learned that there can not be fragment(i mean xml fragment tag not adding dynamically nested fragment) in fragment. But for instance, in the last android update of whatsapp there are three page in one activity and one of them has list fragment(chat list) so i wonder how whatsapp have done that? Thanks.

my xml layout which contains list fragment:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:text="Welcome to newsfeed" />

<Button
    android:id="@+id/nfButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="52dp"
    android:text="@string/signout" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >
        <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/nfListFragment"
            android:name="com.comp.buzz.newsfeed.NFQuestionFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            tools:layout="@layout/item_question" />
        </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

onCreativeView method of main fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_newsfeed, container, false);
        Fragment fragment = new ListFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.swipe_container, fragment).commit();
    return view;
}

Solution

  • You can nest fragments with a few restrictions (according to the doc):

    • Android 4.2 or higher
    • You can nest them only programmatically

    Update 1 Something like this should do the job:

    Fragment fragment = new YourFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.swipe_container, fragment).commit();