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?
ParentFragment parentFrag = ((ParentFragment)ChildFragment. this. getParentFragment()); parentFrag. someMethod();
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() .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With