CollapsingToolbarLayout如何以编程方式展开,动画持续时间是多少?

3
我在我的Android应用程序中使用CollapsingToolbarLayout。我的应用程序的最低API要求是9。
当用户点击折叠的工具栏时,我需要扩展折叠的工具栏,就像最新的Gmail日历应用程序一样。因此,我设置了一个onClick监听器,在其中执行以下操作:
public void onClick(View v) {
     if(toolbarExpanded) {
         mAppBar.setExpanded(false, true);
     } else {
         mAppBar.setExpanded(true, true);
     }
     toolbarExpanded = !toolbarExpanded;
 }

这个功能运行得很好,但我的问题是它运行的动画速度很慢,给用户带来了不好的体验。

有没有办法改变持续时间或定义自定义动画来解决这个问题?

提前感谢您。


1
解决方法是回滚到compile 'com.android.support:design:23.1.0',恐怕只能这样了。这个问题是由Google最近引入的,可以在这里阅读更多信息:https://dev59.com/VVsX5IYBdhLWcg3wY-kh - Konstantin Loginov
2个回答

3

要使用动画扩展,请使用

 AppBarLayout.setExpanded(true,true);

要使用动画折叠,请使用

 AppBarLayout.setExpanded(false,true);

3

注意: 这篇答案基于 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);
    }
}

要获取Behavior,您需要从AppBarLayout的LayoutParams中获取它。
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = params.getBehavior();

1
完美运行,如何编写代码以折叠#Raphael Royer-Rivard? - Anbarasu Chinna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接