Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child fragment replacing parent fragment root layout

I've viewpager with 5 fragments, in one of them I want to completely replace it by button click. I also want to be able to hide child fragment by back button. Here 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 replace contacts_layout like this:

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

it doesn't work (I mean there is no error, but my ImportContactsFragment not showing at all). But when I try replace import_contacts view like this:

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

everything is ok, ImportContactsFragment shown.

So I wonder is it possible to replace all fragment content by child fragment? Maybe I can do it in some other way?

like image 567
Bersh Avatar asked Apr 19 '13 14:04

Bersh


People also ask

How do I get parent fragments from child fragments?

ParentFragment parentFrag = ((ParentFragment)ChildFragment. this. getParentFragment()); parentFrag. someMethod();

Can you have a fragment within a fragment?

Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .

Are fragments reusable?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

Which of the following is used to add fragment inside fragment?

Since FragmentContainerView is a subclass of FrameLayout, a Fragment can be added to it in the same way it can be added to a FrameLayout. Another way of adding a Fragment to a FragmentContainerView is by using the android:name attribute in the XML layout file.


1 Answers

The replace transaction doesn't remove the current views from the target layout container so when you use the first pice of code the new Fragment is added to the contacts_layout LinearLayout but it will not be seen as the previous views cover the entire screen(height).

With the second piece of code the LinearLayout to which you add the new Fragment is the first child of the parent LinearLayout and it has space so its visible.

For what you're doing I advise you to wrap the initial layout in a Fragment class placed in a wrapper layout which you could then later replace very easy.

like image 53
user Avatar answered Nov 16 '22 12:11

user