Ich habe eine Aktivität, die eine komplexe Struktur von Fragmenten haben. Die Aktivität hat zwei Container, jeweils für ein NavigationDrawerFragment und ein Main Content Fragment (HomeTabFragment). Das Main Content Fragment verfügt über eine TabLayout + ViewPager-Struktur mit zwei untergeordneten Fragmenten (Suche und News). Das funktioniert gut, bis ich das Main Content Fragment durch ein anderes Fragment (Settings) ersetzen muss. Das funktionierte auch gut, aber wenn ich zum selben Main Content Fragment zurückkehren möchte, verschwindet der ViewPager zusammen mit den Kindfragmenten.ViewPager verschwindet nach dem Zurückgehen zu dem enthaltenden Fragment
Dies ist Struktur. HomeTabFragment wird durch SettingsFragment über eine Taste (In LeftDrawerFragment) ersetzt und SettingsFragment wieder durch HomeTabFragment über einen Taster ersetzt (In LeftDrawerFragment oder einem Hardware-Zurück-Taste)
HomeActivity
|-LeftDrawerFragment
|-HomeTabFragment
| |-ViewPager
| |-HomeSearchFragment
| |-HomeNewsFragment
|-SettingsFragment
Hier ist die HomeActivity Layout -
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--<include layout="@layout/content_home"/>-->
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Toolbar-->
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center_horizontal"
android:background="?attr/colorPrimary"
android:gravity="center_horizontal"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:subtitle="Your Medi-buddy"
app:title="@string/app_name">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@null"
android:onClick="onHomeMenuClick"
android:padding="5dp"
android:src="@drawable/ic_menu_white" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@android:style/TextAppearance.Holo.Large" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!-- The main content view -->
<FrameLayout
android:id="@+id/tab_frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<!-- The navigation drawer -->
<FrameLayout
android:id="@+id/left_drawer_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
Im Anschluss wird das Layout meiner HomeTabFragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@android:color/white"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_search_white" />
</android.support.design.widget.CoordinatorLayout>
Dies ist, wie ich bin Wechsel hin und her zwischen HomeTabFragment und S ettingsFragment
public void onLeftDrawerSettingsClick(View view) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
getSupportFragmentManager().beginTransaction().replace(R.id.tab_frag_container, mApp.getFragmentController().getSettingsFragment()).addToBackStack("Drawer").commit();
}
public void onLeftDrawerHomeClick(View view) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
getSupportFragmentManager().beginTransaction().replace(R.id.tab_frag_container, mApp.getFragmentController().getHomeTabFragment()).commit();
}
Ich bin nicht in der Lage zu verstehen, warum der ViewPager durch die Ersetzung Transaktion betroffen ist. Ich habe herausgefunden, dass es etwas damit zu tun hat, Child-Fragmente in API Level 15 oder niedriger nicht zuzulassen, aber dies wurde in der neuesten Support-Bibliothek behoben und ich denke nicht, dass es ein Problem sein kann.
Gibt es eine Möglichkeit, das HomeTabFragment durch SettingsFragment und umgekehrt zu ersetzen, ohne die anderen Funktionen zu beeinträchtigen? Oder mache ich etwas falsch?