1

Ich verwende eine einzelne Aktivität mit vier Fragments. Fragment , und ist Viewpager Fragments und Fragment 4 a Detail Fragment ist, das 3-Fragment ersetzt, wenn ein Listenelement angeklickt wird. Wenn das Detailfragment das Masterfragment ersetzt, fügt es leider eine Toolbar und Zurück-Schaltfläche zusätzlich zu der vorhandenen Symbolleiste und Tablayout hinzu. Allerdings möchte ich stattdessen die vorhandene Symbolleiste und das Tablayout durch diese ersetzen.Symbolleiste ausblenden und tablayout von einem bestimmten Fragment in einem Viewpager

FRAGE: Wie würde ich dies erreichen, ohne eine neue Aktivität zu erstellen?

Masterfragment. Hat gewünschte Symbolleiste und Tablayout gewünscht.

enter image description here

Einzelheiten Fragment. Hat sowohl neue Symbolleiste (gewünscht) und Original-Werkzeugleiste und tablayout (unerwünschten)

enter image description here

MainActivity.XML

<?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" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    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:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:background="@color/black_process_2"/> 

</android.support.design.widget.AppBarLayout> 

DetailFragment.XML

<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" 
tools:context="com.allison.viewpagermasterdetail.ItemDetailActivity" 
tools:ignore="MergeRootFrame"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/app_bar_height" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     app:toolbarId="@+id/toolbar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/detail_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" > 

     </android.support.v7.widget.Toolbar> 

    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/item_detail_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <TextView 
     android:id="@+id/item_detail" 
     style="?android:attr/textAppearanceLarge" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="16dp" 
     android:textIsSelectable="true"/> 

</android.support.v4.widget.NestedScrollView> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|start" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/stat_notify_chat" 
    app:layout_anchor="@+id/item_detail_container" 
    app:layout_anchorGravity="top|end" /> 

Antwort

2

Beim Öffnen von Details Fragment invoke:

toolbar.setVisibility(View.GONE); 
tabLayout.setVisibility(View.GONE); 

und

@Override 
public void onBackPressed() { 
    if (currentFragment instanceOf DetailFragment) { 
     toolbar.setVisibility(View.VISIBILE); 
     tabLayout.setVisibility(View.VISIBLE); 
    } 
    ... 
} 
1

Nun, ich persönlich denke, dass die beste Lösung, das Detail in einer separaten Aktivität mit einem eigenen Symbolleiste zu öffnen ist. Diese Lösung wäre meiner Meinung nach die einfachste und die sauberste.

Wenn Sie für das Detail Fragment gehen würde ich vorschlagen, dass Sie Symbolleiste in der Aktivität über setSupportActionBar() festlegen und ändern Sie es innerhalb des Fragments mit getSupportActionBar() -Methode.

+0

Ich schätze Ihre Antwort, aber es ist ein bisschen frustrierend, dass die einzige Spezifikation meiner Frage ignoriert wurde. –

+0

Nun, ich reagierte auf die Variante ohne Detailaktivität auch :). Ich würde die Symbolleiste mit getSupportActionBar() anpassen und das Tablayout ausblenden, wenn der Benutzer zum Detailfragment wechseln will. Außerdem würde ich die Symbolleiste aus dem Detailfragment-Layout entfernen. –