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"
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() {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With