androidandroid-scroll

Android: unable to hide appbar on scrolling


I am trying hide appbar when scrolling but it does not work . This is the main 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"
        tools:context=".MainActivity">
        <data>
            <variable
                name="user"
                type="com.userdashboard.model.User_dash" />
        </data>
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <include
                    android:id="@+id/content"
                    layout="@layout/appbar_user_dash"
                    bind:user="@{user}"/>


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

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

                    </LinearLayout>

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

           </LinearLayout>

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

</Layout>

I included appbar in main 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">

    <data>

        <variable
            name="user"
            type="com.userdashboard.model.User_dash" />
    </data>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="255dp"
        android:background="#181F29">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_margin="16dp"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/ic_share" />

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_margin="16dp">

                    <ImageView
                        android:id="@+id/imv_setting"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:contentDescription="@string/app_name"
                        android:src="@drawable/ic_settings" />

                    <ImageView
                        android:id="@+id/imv_message"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toEndOf="@id/imv_setting"
                        android:contentDescription="@string/app_name"
                        android:src="@drawable/ic_message" />
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:layout_marginTop="104dp"
                    android:background="@drawable/curve_appbar"
                    android:gravity="end"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_marginEnd="140dp"
                        android:hint="userName"
                        android:text="@{user.firstName}"
                        android:textColor="@android:color/white"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginEnd="140dp"
                        android:gravity="end"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:hint="1398/06/21"
                            android:paddingEnd="8dp"
                            android:text="@{user.createDate}"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingEnd="8dp"
                            android:text="date join:"
                            android:textColor="#616E81"
                            android:textSize="16sp" />


                        <ImageView
                            android:id="@+id/imageView"
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:contentDescription="@string/app_name"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_datetime" />


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginEnd="140dp"
                        android:gravity="end"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:hint="test"
                            android:paddingEnd="8dp"
                            android:text="@{user.jobLocations.name}"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingEnd="8dp"
                            android:text="service:"
                            android:textColor="#616E81"
                            android:textSize="16sp" />

                        <ImageView
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:contentDescription="@string/app_name"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_service_location" />

                    </LinearLayout>

                </LinearLayout>

                <ImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="80dp"
                    android:layout_marginEnd="50dp"
                    android:background="@drawable/image_border"
                    android:contentDescription="@string/app_name"
                    android:src="@{user.userImage}" />

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="20dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="148dp"
                    android:layout_marginEnd="46dp"
                    android:background="#CD181F29">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="pic"
                        android:textColor="@android:color/white"
                        android:textDirection="rtl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="80dp"
                    android:layout_height="25dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="175dp"
                    android:layout_marginEnd="46dp"
                    android:background="@drawable/round_edge_shape">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="@{user.user_JobLocations}"
                        android:textColor="@android:color/white"
                        android:textDirection="rtl"
                        android:textStyle="bold" />

                </RelativeLayout>

            </RelativeLayout>

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

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

</layout>

I am using android data binding as you can see in my xml code but I added app:layout_scrollFlags="scroll|enterAlways" to toolbar and app:layout_behavior="@string/appbar_scrolling_view_behavior" to the NestedScrollView in main layout but when I ran the application, the toolbar was not hidden on scrolling. What is wrong here?


Solution

  • Thanks @MikeM I changed main layout to this:

    <?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"
        tools:context=".MainActivity">
        <data>
            <variable
                name="user"
                type="com.userdashboard.model.User_dash" />
        </data>
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <include
                android:id="@+id/content"
                layout="@layout/appbar_user_dash"
                bind:user="@{user}" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginTop="255dp">
    
                <android.support.v4.widget.NestedScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">...
    

    And now appbar is scrolled :-)