How to show collapsed elements in CoordinatorLayout programmatically?
Asked Answered
T

3

9

I have CoordinatorLayout as described in blog: http://android-developers.blogspot.ru/2015/05/android-design-support-library.html

<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.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

  <android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
     <android.support.v7.widget.Toolbar
              ...
              app:layout_scrollFlags="scroll|enterAlways">

     <TextView
              ...
              app:layout_scrollFlags="scroll|enterAlways">
   </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Inside AppBarLayout I have Toolbar and TextView with additional status info. AppBarLayout can be collapsed (after scrolling). Sometimes I need to show AppBarLayout in order to show changed status.

How to do it programmatically?

Trilobite answered 10/6, 2015 at 12:19 Comment(0)
G
30

As mentioned in other comment:

Using support libs v23 you can call appBarLayout.setExpanded(true/false)

Girovard answered 4/12, 2015 at 9:56 Comment(1)
why this is not accepted as answer? works perfectly.Cnemis
M
2

Due to answear from Tuấn Trần Anh, founded here, you can use this two methods to collapse and expend CoordinatorLayout programaticly:

public void collapseToolbar(){
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
    behavior = (AppBarLayout.Behavior) params.getBehavior();
    if(behavior!=null) {
        behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
    }
}

public void expandToolbar(){
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
    behavior = (AppBarLayout.Behavior) params.getBehavior();
    if(behavior!=null) {
        behavior.setTopAndBottomOffset(0);
        behavior.onNestedPreScroll(rootLayout, appbarLayout, null, 0, 1, new int[2]);
    }
}
Maulstick answered 28/6, 2015 at 20:7 Comment(0)
M
0

In my case i use this solution

 public void resetAppBarLayout() {
    if (mContentContainer != null) {
        final AppBarLayout.ScrollingViewBehavior container_behavior = ((AppBarLayout.ScrollingViewBehavior)
                ((CoordinatorLayout.LayoutParams) mContentContainer.getLayoutParams()).getBehavior());
        if (container_behavior != null) {
            container_behavior.setTopAndBottomOffset(appBarLayout.getTotalScrollRange());
        }
        final AppBarLayout.Behavior appbar_behavior = ((AppBarLayout.Behavior)
                ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior());
        if (appbar_behavior != null) {
            appbar_behavior.setTopAndBottomOffset(0);
        }
    }
}

for force showing appbarlayout. Reset scrolled view to appbarLayout max scroll range and appBarLayout to start position = 0.

Mellar answered 17/7, 2015 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.