12

Below is the code of my coordinator layout. It works well.

 <android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:contentScrim="#f4f4f4"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/echo3"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentBottom="true"
                    android:background="#fff">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="Private Albums"
                        android:textColor="#000"
                        android:textSize="22sp"
                        android:textStyle="bold" />
                </RelativeLayout>

            </RelativeLayout>

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/anim_toolbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/content_profile" />


        <!--<fragment-->
        <!--android:id="@+id/detail"-->
        <!--android:name="<package>.<fragment_name>"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->

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

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

But I want to fade the whole relative layout as I scroll up it should started fading(Mean whole collapse bar layout). It do fade out at a specific point but I want it to fade out as I scroll up. Thanks, Any help is appreciated.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Asad AndyDev
  • 481
  • 5
  • 23

2 Answers2

45

You can reduce the alpha of the RelativeLayout as you scroll up by hooking up an AppBarLayout.OnOffsetChangedListener to the AppBarLayout. Below is the code I used in my app.

appBar = (AppBarLayout) findViewById(R.id.app_bar_layout);
appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            relativeLayoutToFadeOut.setAlpha(1.0f - Math.abs(verticalOffset / (float)
                    appBarLayout.getTotalScrollRange()));


        }
    });
ChinLoong
  • 1,735
  • 24
  • 26
-1

Please refer below code, for me its working fine..

 <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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_profile_parent_layout"
        android:background="@color/splash_bg_color"
        tools:context=".Activity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_barLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/view_profile_profile_app_bar_height"
            android:elevation="@dimen/view_profile_app_bar_elevation"
            android:theme="@style/ThemeOverlay.AppCompat.Light">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:titleEnabled="false"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="exitUntilCollapsed|scroll"
                app:title="Collapsing Toolbar">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="?attr/actionBarSize">

                <de.hdodenhof.circleimageview.CircleImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/view_profile_imageView"
                    android:layout_width="@dimen/view_profile_profile_width"
                    android:layout_height="@dimen/view_profile_profile_height"
                    android:src="@drawable/ic_profile_icon"
                    android:layout_centerHorizontal="true"
                    app:civ_border_width="@dimen/view_profile_profile_border_width"
                    app:civ_border_color="@color/textPrimary"/>

                <TextView
                    android:id="@+id/view_profile_profile_name_textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/view_profile_profile_name_margin_top"
                    android:layout_below="@id/view_profile_imageView"
                    android:fontFamily="@font/roboto_regular"
                    android:text=""
                    android:textSize="@dimen/view_profile_profile_name_text_size"
                    android:gravity="center"
                    android:textColor="@color/white"/>

                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    app:layout_collapseMode="pin"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="@dimen/login_btn_elevation"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    android:contentInsetStart="0dp"
                    android:contentInsetLeft="0dp">

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

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

        </android.support.design.widget.AppBarLayout>
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46