Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

params.getBehaviour() returns null value

I was playing with the new Android Design Library. The CollapsingToolbarLayout works perfectly. However, I am having trouble setting default state of toolbar as Collapsed.

I am trying to implement the solution shown here and here

I am calling following code in my onResume of Activity:

   CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if(behavior!=null)
    {
        Log.d("DEBUG", "Behaviour is Not Null ");
        int[] consumed = new int[2];
        behavior.onNestedPreScroll(coordinator, appBarLayout, null, 0, 1000,consumed);
//      behavior.onNestedFling(coordinator, appBarLayout, null, 0, 10000, true);
     }
     else
          Log.d("DEBUG", "Behaviour is Null " );

However, the behaviour returned by params is null. My xml is code same as here, except i am not using drawer and CordinatorLayout is my root Layout.

EDIT: I earlier tried switching AppBarLayout.Behavior to AppBarLayout.ScrollingViewBehavior and setting layout_behavior for AppBarLayout to @string/appbar_scrolling_view_behavior, but it resulted in weird layout.

Robin's answer works nicely. To Complement that, the behavior can also set in xml using following tag in AppBarLayout:

app:layout_behavior="android.support.design.widget.AppBarLayout$Behavior"
like image 470
User31689 Avatar asked Jun 26 '15 07:06

User31689


1 Answers

You have to set a layout behavior before.

Just use following code at your onCreate method:

((CoordinatorLayout.LayoutParams) YOUR_LAYOUT.getLayoutParams()).setBehavior(new AppBarLayout.Behavior() {});

like image 189
Robin Achenbach Avatar answered Sep 30 '22 14:09

Robin Achenbach