How to collapse ImageView nested inside a CoordinatorLayout - android

I have the following layout
<CoordinatorLayout>
<CardView>
<LinearLayout>
<ImageView />
<View />
</LinearLayout>
</CardView>
<RecyclerView />
</CoordinatorLayout>
I just want to hide the ImageView with parallax animation whenever the RecyclerView is scrolled down and show the ImageView whenever RecyclerView is scrolled up. I got to know that this behaviour can be achieved using CoordinatorLayout.

For a parallax effect you need to use AppBarLayout and CollapsingToolbarLayout.
You can try putting ImageView with attribute
app:layout_collapseMode="parallax"
inside the CollapsingToolbarLayout above the Toolbar
Detailed code with CoordinatorLayout
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:statusBarBackground="#color/colorPrimaryDark"
android:fitsSystemWindows="true"
tools:context="com.example.android">
<android.support.design.widget.AppBarLayout android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true"
android:elevation="4dp"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleEnabled="false"
android:background="#color/colorPrimaryDarkTransparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="208dp"
android:scaleType="centerCrop"
android:src="#drawable/image_you_want"
app:layout_collapseMode="parallax"
android:background="#color/colorPrimaryDark"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:background="#color/colorPrimaryDarkTransparent2"
app:titleTextAppearance="#style/AppBarTitleappearance"
app:subtitleTextAppearance="#style/AppBarSubtitleApppearanceLight"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#color/white"
app:tabSelectedTextColor="#color/white"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabGravity="center"
android:elevation="4dp"
app:tabIndicatorColor="#color/ornage"
app:tabTextAppearance="#style/NavigationTabTextAppeareance"
/>
</android.support.design.widget.AppBarLayout>
Make sure you have following namespaces in your root tag of the layout
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
As per your requirement you can Use any layout at that position with attribute
app:layout_collapseMode="parallax"
I hope you will get some help from this.

The view you do want to collapse must be wrapped within CollapsingToolbarLayout inside AppBarLayout.
Try the structure below:
<....CoordinatorLayout
....
...
<android.support.design.widget.AppBarLayout
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="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- Your collapsing content will goes here -->
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<...NestedScrollView
.....
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</....CoordinatorLayout>

Related

Android: CollapsingToolBar do not setting at one level the icons with the title that appears in the bottom of toolbar after collapse

Screen after collapsed
Using CollapsedToolbarLayout: when picture is hiden and collapsed, the title is going to the top(in toolbar), but not on the same level(line) with the back(and menu) icons. Where can there be a mistake? (Everything is put inside CoordinatorLayout with width and height = match_parent and fitsSystemWindows="true")
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="Hello"
app:collapsedTitleGravity="center_vertical"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:background="#color/greenMain"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
menu="#{R.menu.menu_profile}"
navigationIcon="#{R.drawable.ic_small_arrow_left}"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

CoordinatorLayout isn't scrolling after nestedscrollview or recyclerview removing

When switching tabs I hide (view.removeAllViews() or INVISIBLE or GONE) the content of the old tab and show the content of the new tab.
The content (#+id/container) of each tab contains Nestedscrollview or Recyclerview.
When you open the form everything works fine.
After switching, the form scrolling does not work when you touch
AppBarLayout
After scrolling for Nestedscrollview or Recyclerview, scrolling for
AppBarLayout starts working.
I'm putting this in CoordinatorLayout:
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_list_items"
android:background="#drawable/product_detail_background"
android:fitsSystemWindows="true"
app:theme="#style/ProductDetailsAppBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/product_detail_background"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:scrimAnimationDuration="100">
<LinearLayout
android:id="#+id/ll_app_bar_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/dim_8"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
app:layout_collapseMode="none">
...
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:scrollbarFadeDuration="100"
app:layout_collapseMode="pin">
...
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tl_product"
style="#style/TabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_list_items">
...
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/fab_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|end"
android:padding="#dimen/margin_fab"
app:menu_backgroundColor="#color/fab_transparent_background"
app:menu_colorNormal="#color/main_green"
app:menu_colorPressed="#color/main_green"
app:menu_icon="#drawable/ic_add">
...
</com.github.clans.fab.FloatingActionMenu>

How to stick a view element below a collapsingtoolbarlayout toolbar in android?

trying to build an Android app with a collapsingtoolbarlayout and a view element that always sticks directly below the bottom of the toolbar.
I think I'm just messing up my organization within the whole appbarlayout, but I can't seem to figure out the right ordering.
Any help would be great!
Thanks-
Here's my Activity xml
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="244dp"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="#style/CollapsedTheme"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="center|bottom"
app:expandedTitleMarginBottom="20dp"
app:expandedTitleMarginEnd="20dp"
app:expandedTitleMarginStart="20dp"
app:expandedTitleTextAppearance="#style/ExpandedTheme"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:theme="#style/ToolbarColoredBackArrow">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical">
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_product" />
And I'm trying to add in
<RelativeLayout
android:layout_below="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/blue"/>
So that it always stays below the toolbar, but when the collapsingtoolbarlayout scrolls to the top of the screen, it stays sticking below it.
An AppBarLayout extends LinearLayout - just add your RelativeLayout below your CollapsingToolbarLayout and don't include any layout_scrollFlags.
<AppBarLayout>
<CollapsingToolbarLayout />
<RelativeLayout />
</AppBarLayout>

AppBarLayout change offset programmatically

How can I change the offset of the AppBarLayout programmatically?
I want a certain offset to the AppBarLayout(partially expanded) when the Activity first loads, then the user could expand it more or collapse it. The current behaviour is that it's fully expanded when the Activity first loads.
my.xml
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="300dp"
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:clickable="true"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_collapseParallaxMultiplier="0.7"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/pic"/>
<View
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/overlay"
app:layout_collapseMode="pin"/>
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/app_name"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Need to support API 16+
Thanks!
Use this: ScrollableAppBar
You just have to replace your AppBarLayout with:
<it.michelelacorte.scrollableappbar.ScrollableAppBar
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="380dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
</it.michelelacorte.scrollableappbar.ScrollableAppBar>
and than:
ScrollableAppBar appBarLayout = (ScrollableAppBar) findViewById(R.id.appbar);
//To give the effect "in the middle" of the image (like gif)
appBarLayout.collapseToolbar();
Output:

CollapsingToolbarLayout doesn't work well with NestedScrollView

I made an activity where image should collapse with toolbar and only text below would be visible. But When Toolbar being collapsed, there shows up a large gap between Toolbar and NestedScrollView
<android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:background="#color/white"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
app:elevation="5dp"
app:layout_collapseMode="pin"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarId="#+id/toolbar"
android:id="#+id/collapsing"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.noframe.farmisagronom.util.ResizibleImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:minHeight="100dp"
android:maxHeight="400dp"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
android:scaleType="centerCrop"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nesteview"
app:layout_anchorGravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
.....
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
And this activity looks really good.
But when i scroll up the NestedScrollView this goes out of control.
This little space between toolbar and text is getting on my nerves.
note that if there is large text in NestedScrollView problem won't show up, but if NestedScrollView + collapsed tool bar doesn't take all phone screen, then there is a gap between them.
Adding:
android:layout_gravity="fill_vertical"
to the NestedScrollView worked for me.

Categories

Resources