Reputation: 1509
I have an activity with some fragments which has a CollapsingToolbarLayout
. In an specific fragment, I want the toolbar to show a TabLayout
. I have made it work.. well almost.
When changing to the fragment where TabLayout
must be shown, the CollapsingToolbarLayout
is collapsed using:
collapsingToolbarLayout.setExpanded(false,true);
And the tabs are shown using this in the fragment code:
tabs.setVisibility(View.VISIBLE);
The issue is that the CollapsingToolbarLayout
is completely hidden and it only shows the tab bar, instead of the collapsed toolbar above the app bar.
This is the layout:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:elevation="0dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="24dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/toolbarHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/header"
android:visibility="invisible"
app:layout_collapseMode="parallax"
tools:ignore="ContentDescription"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="0dp"
android:minHeight="?actionBarSize"
android:paddingTop="@dimen/tool_bar_top_padding"
android:transitionName="actionBar"
app:contentInsetStart="@dimen/toolbar_contentInset"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:ignore="UnusedAttribute">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text=""
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textColor="#fff"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/primary"
android:clipToPadding="false"
android:minHeight="?actionBarSize"
android:paddingLeft="@dimen/tabs_contentInset"
android:paddingStart="@dimen/tabs_contentInset"
android:visibility="gone"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/semitransparent_white"
tools:ignore="NewApi,RtlSymmetry,UnusedAttribute"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
And this is how it currently looks like, (and which I don't want it to be).
And this is how I expect/want it to look like:
Thanks in advance for any help.
Upvotes: 2
Views: 1888
Reputation: 615
I already answer to this problem in here = https://stackoverflow.com/a/54817940/4258880. But you can add an offset change listener to explicitly set your action bar when you reach a greater than percentage. (Sometimes I dont know why, the offset changes on scroll behavior, even though it has a fixed height)
In Kotlin:
app_bar_layout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
val percentage: Float = Math.abs(verticalOffset) / appBarLayout.totalScrollRange.toFloat()
Log.d("%", "$percentage")
when {
percentage < 0.1 -> (activity as MainActivity).bottom_navigation.visibility = View.GONE
percentage >= 1.0.toFloat() -> (activity as MainActivity).setSupportActionBar(where_toolbar) // This is hack to avoid toolbar disappearing
else -> (activity as MainActivity).bottom_navigation.visibility = View.VISIBLE
}
})
Here I am using it to inflate it in a Fragment when the percetange is greater than 1 (Weird).
Upvotes: 0
Reputation: 124
Hello remove the next code line
app:layout_scrollFlags="scroll|enterAlways|snap"
Upvotes: 0
Reputation: 3819
Please try this :
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="24dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/toolbarHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/header"
android:visibility="invisible"
app:layout_collapseMode="parallax"
tools:ignore="ContentDescription"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="0dp"
android:minHeight="?attr/actionBarSize"
android:paddingTop="@dimen/tool_bar_top_padding"
android:transitionName="actionBar"
app:contentInsetStart="@dimen/toolbar_contentInset"
app:layout_collapseMode="pin"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:ignore="UnusedAttribute">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text=""
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textColor="#fff"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/primary"
android:clipToPadding="false"
android:minHeight="?actionBarSize"
android:paddingLeft="@dimen/tabs_contentInset"
android:paddingStart="@dimen/tabs_contentInset"
android:visibility="gone"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@color/semitransparent_white"
tools:ignore="NewApi,RtlSymmetry,UnusedAttribute"/>
</android.support.design.widget.CollapsingToolbarLayout>
please tell me if it works or not in a comment
Good luck
Upvotes: 1