2016-05-02 11 views
2

Ich habe eine NestedScrollView mit CollapsingToolbarLayout und ich möchte es reibungslos funktionieren lassen.Collapsing Symbolleisten-Layout mit NestedScrollView scrollt nicht glatt

Mein Problem ist, wenn ich vom Inhalt nach oben scrolle, wird meine Collapsing Toolbar nicht automatisch erweitert, sondern blockiert, wenn sie oben ankommt. Dann muss ich noch einmal nach oben scrollen, um die Symbolleiste zu erweitern.

Ich mag würde smooth scrolling up von Inhalten erreichen, die Auto erweitern würde mein CollapsingToolbarLayout

Hier ist mein Code:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/product_detail_main_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:apptools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     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:clipToPadding="true" 
      android:foregroundGravity="bottom|right" 
      android:foregroundTintMode="add" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

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

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

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

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

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/scroll" 
     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="wrap_content" 
     android:orientation="vertical"> 

     /* 
      CONTENT 
     */ 

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

ich einige ähnliche Fragen gefunden, aber keine der Antworten für mich gearbeitet.

+0

Versuchen zu einstellen 'android: fitsSystemWindows = "true"' in 'CoordinatorLayout'. –

+0

Nein, hat nicht funktioniert. – Meemaw

+0

eine Lösung gefunden? – apk

Antwort

2

Welche Art von Inhalt befindet sich in LinearLayout?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    /* 
     CONTENT 
    */ 

</LinearLayout> 

Wenn einer der Inhalt recyclerview ist der Grund, warum Symbolleiste kollabiert reagiert nicht auf eine Rolle ist, weil NestedScrollView nicht AppBarLayout.Behavour Klasse nicht aufrufen, wenn es Scroll-Ereignisse von recyclerview erhalten. Das heißt, wenn ein Scroll in der Recycler-Ansicht stattfindet, wird der Scroll-Fortschritt in der Recycler-Ansicht an NestedScrollView gesendet/übergeben. NestedScrollView empfängt das Bildlaufereignis, tut aber nichts damit.

Innen nestedscrollview Klasse

@overide 
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){ 
     //Do nothing 
} 

Um dies zu überwinden und appbarlayout zeigen/Kollaps, wenn recyclerview Scrollen, nur eine benutzerdefinierte Klasse erstellen, die dispatchNestedPreScroll() NestedScrollView und overide das obige Verfahren und Call-Methode erweitert, die die appbarlayout informiert des Scroll-Ereignisses und reagieren darauf.

public class CustomNestedScrollView extends NestedScrollView{ 

    public CustomNestedScrollView(Context context) { 
    super(context); 
    } 

    public CustomNestedScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    } 

    @overide 
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){ 
    dispatchNestedPreScroll(dx,dy,consumed,null); 
    } 
} 

Und dann verwenden Sie diese Klasse in Ihrer layout.xml

<com.my.package.CustomNestedScrollView 
    android:id="@+id/scroll" 
    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="wrap_content" 
    android:orientation="vertical"> 

    /* 
     CONTENT 
    */ 

</LinearLayout> 

+0

funktioniert nicht für mich –

+0

@MitulVarmora können Sie uns mit Ihrem Layout zur Verfügung stellen, um zu wissen, wo das Problem liegt. –

0

ich dieses Problem durch SmoothAppBarLayout gelöst haben
-Link: https://github.com/henrytao-me/smooth-app-bar-layout

diese Abhängigkeit in Ihrer App hinzufügen Level build.gradle

compile "me.henrytao:smooth-app-bar-layout:25.3.1.0" 

Markieren Sie, dass das Kind von NestedScrollView anklickbar und fokussierbar True sein soll.
Dies ist XML-Layout

<?xml version="1.0" encoding="utf-8"?> 
<layout 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"> 

<data> 
    <variable .../> 
</data> 

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayoutPlaceDetail" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintBottom_toTopOf="@+id/adViewPlaceDetail" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 

     <me.henrytao.smoothappbarlayout.SmoothAppBarLayout 
      android:id="@+id/appBarLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      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" 
       app:contentScrim="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed"> 
       <!--app:layout_scrollInterpolator="@android:anim/accelerate_decelerate_interpolator"--> 

       <ImageView 
        android:id="@+id/imgOfPlace" 
        android:layout_width="match_parent" 
        android:layout_height="200dp" 
        android:adjustViewBounds="true" 
        android:contentDescription="@string/app_name" 
        android:scaleType="fitXY" 
        android:src="@drawable/placeholder" 
        app:layout_collapseMode="parallax" /> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        app:layout_collapseMode="pin" 
        app:layout_scrollFlags="scroll|enterAlways" /> 

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

     </me.henrytao.smoothappbarlayout.SmoothAppBarLayout> 

     <android.support.v4.widget.NestedScrollView 
      android:id="@+id/nestedScrollView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" 
      android:overScrollMode="never" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <android.support.constraint.ConstraintLayout 
       android:id="@+id/constraintLayoutInNestedScroll" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:clickable="true" 
       android:focusable="true" 
       android:focusableInTouchMode="true"> 

       <YOUR CONTENT VIEWS> 

      </android.support.constraint.ConstraintLayout> 

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

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

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/adViewPlaceDetail" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     ads:adSize="SMART_BANNER" 
     ads:adUnitId="..." 
     ads:layout_constraintBottom_toBottomOf="parent" 
     ads:layout_constraintEnd_toEndOf="parent" 
     ads:layout_constraintStart_toStartOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

Verwandte Themen