Search code examples
androidxmlandroid-layoutandroid-studioscrollview

nested scrollView doesn't recognize the toolbar


my project has only one toolbar that surrounded by AppBarLayout and also a NestedScrollView control below it that has a CardView layout surrouned linearlayout. no the problem is that the nested scroll overlap the toolbar like this i show in image enter image description here

and also in run time it pass the app like there is no toolbar like this:

enter image description here

enter image description here

this is my code:

<?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/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <android.support.design.widget.AppBarLayout
      android:id="@+id/appbarLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
      >



<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="7dp"
    app:theme="@style/ThemeOverlay.AppCompat"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"

    android:titleTextColor="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    ></android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"

        >

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

            >
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
            <include layout="@layout/scroll_view_item" />
        </LinearLayout>

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

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

scroll_view_item code is here:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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="wrap_content"
    android:layout_margin="16dp"
    android:elevation="5dp"
    app:cardCornerRadius="5dp">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:orientation="vertical"

    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy Header"
        android:textColor="@android:color/holo_red_light"
        android:textSize="20sp"
        android:textStyle="bold"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/app_text"
        />

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

Solution

  • just surround your code with linearlayout like this:

    <?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/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
       <android.support.design.widget.AppBarLayout
          android:id="@+id/appbarLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:fitsSystemWindows="true"
          >
    
    
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:elevation="7dp"
        app:theme="@style/ThemeOverlay.AppCompat"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    
        android:titleTextColor="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        ></android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.NestedScrollView
    
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true"
    
            >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingTop="10dp"
    
                >
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
                <include layout="@layout/scroll_view_item" />
            </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>