问题描述
我是用新的Android设计库播放。该CollapsingToolbarLayout完美的作品。不过,我有麻烦的设置工具栏默认状态为折叠。
我想实现显示的解决方案,here和<一个href=\"http://stackoverflow.com/questions/30655939/android-programmatically-collapse-or-expand-collapsingtoolbarlayout?lq=1\">here
我打电话下面我活动onResume code:
CoordinatorLayout.LayoutParams PARAMS =(CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
AppBarLayout.Behavior行为=(AppBarLayout.Behavior)params.getBehavior();
如果(行为!= NULL)
{
Log.d(调试,行为不为空);
INT [] =消耗的新INT [2];
behavior.onNested preScroll(协调,appBarLayout,NULL,0,1000,消费);
// behavior.onNestedFling(协调,appBarLayout,NULL,0,10000,真正的);
}
其他
Log.d(调试,行为为空);
不过,PARAMS返回的行为为null。
我的XML是code一样的here,但我不使用抽屉CordinatorLayout是我的根布局。
编辑:刚才我试着切换到AppBarLayout.Behavior和AppBarLayout.ScrollingViewBehavior为AppBarLayout设置layout_behavior为@字符串/ appbar_scrolling_view_behavior,但它导致了奇怪的布局。
罗宾的回答很好地工作。以补充,行为也可以在XML使用以下标记设置AppBarLayout:
应用:layout_behavior =$ android.support.design.widget.AppBarLayout行为
您之前设置的布局行为。
只要使用以下code。在您的onCreate方法:
((CoordinatorLayout.LayoutParams)YOUR_LAYOUT.getLayoutParams())setBehavior(新AppBarLayout.Behavior(){});
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() {});
这篇关于params.getBehaviour()返回null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!