Children's fragment replacing the root root layout

I have a viewpager with 5 fragments, in one of them I want to completely replace it with the click of a button. I also want to be able to hide the child fragment using the back button. Here is this fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/contacts_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null">
<LinearLayout
    android:id="@+id/import_contacts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<include layout="@layout/listview_filter"/>

<Button
    android:id="@+id/btn_my_clusters"
    android:background="@drawable/btn_wide_arrow_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/TEXT"
    android:text="@string/btn_my_clusters_text"
    android:layout_marginBottom="10dp"/>

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="center"/>

<ListView
    android:id="@+id/contacts_list"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"/>
</LinearLayout>

When I try to replace contacts_layoutas follows:

ImportContactsFragment importContactsFragment = new  ImportContactsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.contacts_layout, importContactsFragment).commit();

it does not work (I mean that there is no error, but my ImportContactsFragment does not appear at all). But when I try to replace import_contactsas follows:

ImportContactsFragment importContactsFragment = new  ImportContactsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.import_contacts, importContactsFragment).commit();

everything is fine, ImportContactsFragment is displayed.

So, interestingly, is it possible to replace the entire fragment with a child fragment? Maybe I can do it in some other way?

+5
source share
1 answer

replace , , , Fragment _layout LinearLayout, ().

LinearLayout, Fragment, LinearLayout, , .

, , Fragment, -, .

+4

All Articles