17

EDIT von 2016.01.02: https://stackoverflow.com/a/35132144/3397345CoordinatorLayout Statusleiste Polster verschwindet aus ViewPager 2. Seite

EDIT von 27/01/2016: Bug sollten von Android-Team zur Verfügung gestellt, indem Sie den Code gelöst werden Der Fehler wurde in v23.1.1 nicht behoben. Bisher angebotene Lösungen geben keine transparente Statusleiste (was der Zweck dieses Layouts ist) oder sind zu komplex. Eine neue Bildschirm-Aufzeichnung der Fehler finden Sie hier: https://www.youtube.com/watch?v=76IxhlUx8MQ

EDIT von 23/07/2015: Unterstützung Design Bibliothek v22.2.1 beheben nicht :-(Auch dies geschieht auf Symbolleiste schnelles Return on MainActivity

EDIT von 28/07/2015: verlinkte Frage:. CoordinatorLayout status bar padding disappears during fragment transactions

von Beispiel Repository https://github.com/chrisbanes/cheesesquare ich die ViewPager auf Details Aktivität implementiert haben Es funktioniert, aber die StatusBar verschwindet aus der 2. Seite, wie Sie in der zu sehen Bild nur in Lollipop Geräten, Irgendeine Idee? Ich benutze android:fitsSystemWindows="true", aber es funktioniert nur auf der ersten Seite :-(

enter image description here

activity_detail_viewpager.xml wenn ich hier fitsSystemWindows setzen, StatusBar nicht mehr transparent ist, aber es funktioniert (Statusleiste Polsterung nicht verloren). Aber ich würde es transparent gefällt!

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:overScrollMode="ifContentScrolls"/> 

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

activity_detail_fragment.xml

<CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

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

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginStart="48dp" 
      app:expandedTitleMarginEnd="64dp"> 

      <ImageView 
       android:id="@+id/back_item" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:fitsSystemWindows="true" 
       app:layout_collapseMode="parallax" /> 

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

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

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

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:paddingTop="24dp"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="@dimen/card_margin"> 

       ... 

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

      ... 

     </LinearLayout> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:clickable="true" 
     android:src="@drawable/ic_discuss" 
     app:layout_anchor="@id/appbar" 
     app:layout_anchorGravity="bottom|end" 
     app:borderWidth="0dp"/> 

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

style.xml v21

<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 
</resources> 
+1

Posted Problem an Google: https://code.google.com/p/android/issues/detail?id=180492 - auch ppl von anderen Ausgabe bestätigt – Davidea

Antwort

10

Lösung vorgeschlagen von Android-Team in der Antwort meines berichteten Defekts.Dieser Code sollte endlich funktionieren.

ViewPager mViewPager; 

ViewCompat.setOnApplyWindowInsetsListener(mViewPager, 
     new OnApplyWindowInsetsListener() { 
    @Override 
    public WindowInsetsCompat onApplyWindowInsets(View v, 
      WindowInsetsCompat insets) { 
     insets = ViewCompat.onApplyWindowInsets(v, insets); 
     if (insets.isConsumed()) { 
      return insets; 
     } 

     boolean consumed = false; 
     for (int i = 0, count = mViewPager.getChildCount(); i < count; i++) { 
      ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets); 
      if (insets.isConsumed()) { 
       consumed = true; 
      } 
     } 
     return consumed ? insets.consumeSystemWindowInsets() : insets; 
    } 
}); 
+0

Awesome thanks! – Ale

0

Ich lief in ein ähnliches Problem (mit v23.0.1). In meinem Fall tritt das Problem auch auf der ersten Seite auf. Die Problemumgehung, auf die ich mich einigte, bestand darin, die Auffüllung und Höhe der Symbolleiste anzupassen, wenn das Fragment erstellt wird.

// in onCreateView: adjust toolbar padding 
    Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbar); 
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0); 
    toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight(); 

    return mRootView; 
} 

public int getStatusBarHeight() { 
    int result = 0; 
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
} 
+0

Nizza, dann ist es ein Anfang, und etwas beginnt arbeiten (als Korrektur), aber Scrollen nach links und rechts passiert es auf einigen Seiten, um diese Polsterung jetzt überflüssig zu haben, gibt es eine Möglichkeit zu wissen, ob die Statusleiste Padding wieder sichtbar ist, um zu vermeiden, die Höhe der Statusleiste hinzuzufügen? Wir sollten die tatsächliche Höhe der Statusleiste abrufen. Ich vermute. – Davidea

-1

scheint die Lösung für diese einfache

ist haben Sie

android:fitsSystemWindows="true" 

Im Fragment CoordinateLayoutView

es entfernen von hier und in der Tätigkeit der Stammansicht setzen.

Dies sollte jetzt alle Arbeiten

+0

Hallo, der Zweck ist es, es transparent zu haben, daher kann diese Lösung nicht akzeptiert werden. Ich denke, Design-Bibliothek funktioniert für normale und einfache Fälle, während es große Probleme hat (und nicht nur das in meiner Frage beschrieben) in komplexen Fällen. Ich bin fast da, um diese Bibliothek im Falle eines Viewpagers zu verlassen. – Davidea

+0

Bitte zeigen Sie mir den Code, ich habe transparent/Statusleiste farbig, wenn das ist das Bild auf GoogleCode dann sieht es aus wie Sie Android haben können: fitsSystemWindows = "True" an ein paar Orten – RuAware

3

Wie sidecarcat, lief ich in ein ähnliches Problem (mit v23.1.1). Ich poste hier eine Problemumgehung, indem ich den Code von sidecarcat benutze und in einigen Fällen etwas Code hinzufüge, um überflüssige Auffüllung zu entfernen.

// in onCreateView: adjust toolbar padding 
    final int initialToolbarHeight = mToolbar.getLayoutParams().height; 
    final int initialStatusBarHeight = getStatusBarHeight(); 
    mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      int[] locToolbar = new int[2]; 
      mToolbar.getLocationOnScreen(locToolbar); 
      int yToolbar = locToolbar[1]; 
      int topPaddingToolbar = mToolbar.getPaddingTop(); 
      if (isAdded()) { 
       //normal case : system status bar padding on toolbar : yToolbar = initialStatusBarHeight && topPaddingToolbar = 0 
       //abnormal case : no system status bar padding on toolbar -> toolbar behind status bar => add custom padding 
       if (yToolbar != initialStatusBarHeight && topPaddingToolbar == 0) { 
        mToolbar.setPadding(0, initialStatusBarHeight, 0, 0); 
        mToolbar.getLayoutParams().height = initialToolbarHeight + initialStatusBarHeight; 
       } 
       //abnormal case : system status bar padding and custom padding on toolbar -> toolbar with padding too large => remove custom padding 
       else if (yToolbar == initialStatusBarHeight && topPaddingToolbar == initialStatusBarHeight) { 
        mToolbar.setPadding(0, 0, 0, 0); 
        mToolbar.getLayoutParams().height = initialToolbarHeight; 
       } 
      } 
     } 
    }); 

    return mRootView; 
} 

public int getStatusBarHeight() { 
    int result = 0; 
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
}