Android 子片段替换父片段根布局
我有5个片段的viewpager,在其中一个片段中,我想通过单击按钮完全替换它。我还希望能够通过后退按钮隐藏子片段。 以下是此片段布局:Android 子片段替换父片段根布局,android,android-fragments,android-viewpager,Android,Android Fragments,Android Viewpager,我有5个片段的viewpager,在其中一个片段中,我想通过单击按钮完全替换它。我还希望能够通过后退按钮隐藏子片段。 以下是此片段布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/cont
<?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>
它不工作(我的意思是没有错误,但我的ImportContactsFragment根本没有显示)。但当我尝试替换import\u contacts
时,如下所示:
ImportContactsFragment importContactsFragment = new ImportContactsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.contacts_layout, importContactsFragment).commit();
ImportContactsFragment importContactsFragment = new ImportContactsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.import_contacts, importContactsFragment).commit();
如图所示,一切正常
所以我想知道是否有可能用子片段替换所有片段内容?也许我可以用其他方法来做?替换事务不会从目标布局容器中删除当前视图,因此当您使用第一张代码时,新的
片段将添加到联系人布局线性布局中
,但不会被视为之前的视图覆盖了整个屏幕(高度)
在第二段代码中,添加新的片段的线性布局
是父线性布局
的第一个子项,并且它具有可见的空间
对于您正在做的事情,我建议您将初始布局包装在一个片段类中,该类放置在一个包装布局中,稍后您可以很容易地替换它