注意:此答案是基于android设计库v25.0.0。
您可以用反射调用NestedScrollView的AppBarLayout.Behavior的私有方法animateOffsetTo。此方法具有影响动画持续时间的速度参数。
private void expandAppBarLayoutWithVelocity(AppBarLayout.Behavior behavior, CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, float velocity) {
try {
//With reflection, we can call the private method of Behavior that expands the AppBarLayout with specified velocity
Method animateOffsetTo = AppBarLayout.Behavior.getClass().getDeclaredMethod("animateOffsetTo", CoordinatorLayout.class, AppBarLayout.class, int.class, float.class);
animateOffsetTo.setAccessible(true);
animateOffsetTo.invoke(behavior, coordinatorLayout, appBarLayout, 0, velocity);
} catch (Exception e) {
e.printStackTrace();
//If the reflection fails, we fall back to the public method setExpanded that expands the AppBarLayout with a fixed velocity
Log.e(TAG, "Failed to get animateOffsetTo method from AppBarLayout.Behavior through reflection. Falling back to setExpanded.");
appBarLayout.setExpanded(true, true);
}
}
要获得行为,您需要从AppBarLayout的LayoutParams中获取它。
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = params.getBehavior();
解决办法是回滚到'编译“com.android.support:design:23.1.0关闭“我害怕。”这种行为最近由google引入,在这里阅读更多:http://stackoverflow.com/questions/33892376/appbarlayout-setexpandedboolean-true-weird-animation-in-support-library-23-1 –