params.getBehaviour() returns null value

℡╲_俬逩灬. 提交于 2019-12-22 09:18:27

问题


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"

回答1:


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() {});



来源:https://stackoverflow.com/questions/31067082/params-getbehaviour-returns-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!