Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show collapsed elements in CoordinatorLayout programmatically?

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?

like image 754
tse Avatar asked Jun 10 '15 12:06

tse


People also ask

How do I collapse collapsing toolbar programmatically?

How do I collapse collapsing Toolbar programmatically? Use mAppBarLayout. setExpanded(true) to expand Toolbar and use mAppBarLayout. setExpanded(false) to collapse Toolbar.

How do I know if my collapsed toolbar is collapsing?

To detect if fully collapsed check if Math. abs(offset) == appBarLayout.

How do you align items in coordinator layout?

You can try gravity to align inside the CoordinatorLayout. android:layout_gravity="end" This worked for me. Save this answer. Show activity on this post.

What is Layout_anchor?

app:layout_anchor: This attribute can be set on children of the CoordinatorLayout to attach them to another view. The value would be the id of an anchor view that this view should position relative to. Note that, the anchor view can be any child View (a child of a child of a child of a CoordinatorLayout, for example).


2 Answers

As mentioned in other comment:

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

like image 51
Mario Velasco Avatar answered Sep 19 '22 16:09

Mario Velasco


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]);
    }
}
like image 30
Anton A. Avatar answered Sep 20 '22 16:09

Anton A.