Search code examples
androidandroid-collapsingtoolbarlayoutandroid-nestedscrollview

Android App bar scrolling with overlapping content in Flexible space


In my layout am using App bar scrolling with overlapping content in Flexible space

I have a buttons list in Nested Scroll view in a horizontal scroll view, I want to stop that horizontal scrollable buttons layout at the top (below of the action bar) when I was scrolling up. How to achieve this, please help?

This the structure of my layout:

<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="RtlHardcoded">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:contentScrim="?colorPrimary"
            app:expandedTitleMarginBottom="94dp"
            app:expandedTitleTextAppearance="@style/CollapsingTextAppearance.Inverse"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="@string/app_name">

            // conatins my header view

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_overlapTop="38dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <android.support.v7.widget.CardView
                android:id="@+id/cardview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="@dimen/margin"
                app:cardBackgroundColor="@color/white"
                android:focusableInTouchMode="false"
                android:focusable="false"
                app:cardCornerRadius="10dp"
                app:cardElevation="4dp"
                app:contentPaddingBottom="16dp"
                app:contentPaddingLeft="0dp"
                app:contentPaddingRight="0dp">

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

                    <HorizontalScrollView
                        android:id="@+id/horizontalSV"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fillViewport="true"
                        android:scrollbarSize="2dp">

                        .... // conatins buttons

                        .... // conatins my list view view

                        </HorizontalScrollView>

                    </LinearLayout>


                </android.support.v7.widget.CardView>
        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
    </android.support.design.widget.CoordinatorLayout>

See attached screenshot for your ref.

enter image description here


Solution

  • Change your code to this.cut your code and use scroll behavior in cardview

    <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:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="RtlHardcoded">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            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"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                app:contentScrim="?colorPrimary"
                app:expandedTitleMarginBottom="94dp"
                app:expandedTitleTextAppearance="@style/CollapsingTextAppearance.Inverse"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:title="@string/app_name">
    
                // conatins my header view
    
                </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
         <android.support.v7.widget.CardView
                    android:id="@+id/cardview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/margin"
                    app:cardBackgroundColor="@color/white"
                    android:focusableInTouchMode="false"
                    android:focusable="false"
                    app:cardCornerRadius="10dp"
                    app:cardElevation="4dp"
                    app:contentPaddingBottom="16dp"
                    app:contentPaddingLeft="0dp"
                    app:contentPaddingRight="0dp">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
                        <HorizontalScrollView
                            android:id="@+id/horizontalSV"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fillViewport="true"
                            android:scrollbarSize="2dp">
    
                            .... // conatins buttons
    
                            .... // conatins my list view view
    
                            </HorizontalScrollView>
    
                        </LinearLayout>
    
         <android.support.v4.widget.SwipeRefreshLayout
          android:id="@+id/swipeLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:behavior_overlapTop="38dp"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
          <android.support.v4.widget.NestedScrollView
           android:id="@+id/nestedScrollView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:scrollbars="none">
    
    
          </android.support.v4.widget.NestedScrollView>
         </android.support.v4.widget.SwipeRefreshLayout>
    
                    </android.support.v7.widget.CardView>
    
    
        </android.support.design.widget.CoordinatorLayout>