Below is the code of my coordinator layout. It works well.
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()));
}
});
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>