Issue
I am working on an app using a CollapsingToolbarLayout, with an ImageView inside it. I wanted to add a gradient on top of it to look nicer and to be able to read the CollapsingToolBar title better, so I made a little hack and added a Relative Layout with a textview inside it, then I added a background to that same TextView (which is the gradient I was talking about). The problem with this is that when the ToolBar is collapsed, the gradient still shows over it and I dont want it to happen, how can I make it invisible when the ToolBar is collapsed?
Design:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".anime_page">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="450dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
app:collapsedTitleTextAppearance="@style/collapsedToolbarLayoutTitleColor"
app:expandedTitleTextAppearance="@style/expandedToolbarLayoutTitleColor"
android:theme="@style/Theme.AnimeWatcher"
android:id="@+id/anime_page_collapsing_toolbar">
<ImageView
android:id="@+id/anime_page_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" >
<ImageView
android:id="@+id/anime_page_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_arrow_back_24"
android:paddingEnd="10dp" />
</androidx.appcompat.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="@drawable/black_gradient" />
</RelativeLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/anime_page_rcv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
</androidx.recyclerview.widget.RecyclerView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Images of the design:
Solution
Add an OnOffsetChangedListener
to the AppBar
and listen to the changes when it's collapsed or expanded and hide/show your TextView
based on that.
Add an ID to the appbar
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="450dp"
android:fitsSystemWindows="true">
Then, access the AppBar
and add an OnOffsetChangedListener
as
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
float percentage = (float) Math.abs(verticalOffset) / appBarLayout.getTotalScrollRange();
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
// Collapsed
//Hide your TextView here
tv.setVisibility(View.GONE);
} else if(verticalOffset == 0) {
//Expanded
//Show your TextView here
tv.setVisibility(View.VISIBLE);
} else {
//In Between
tv.setVisibility(View.VISIBLE);
tv.animate().alpha(percentage);
}
});
You can choose to play with the alpha
property only instead of visibility
, that's upto you.
Answered By - Lalit Fauzdar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.