【问题标题】:How do I shift content up when I am animating Toolbar out?动画工具栏时如何向上移动内容?
【发布时间】:2015-01-19 20:35:48
【问题描述】:

我有一个与 Play 商店相同的布局,其中我有一个工具栏、选项卡条和 ViewPager,它们都在一个垂直的线性布局中。我想实现工具栏隐藏但 TabStrip 和 ViewPager 保留但与工具栏一起动画的 Play 商店的快速返回模式。

我使用 animate().translateY() 将动画工具栏部分向下移动,但我无法让内容随之向上移动(至少不顺利)。我尝试过类似的方法:

<FrameLayout>

    <Toolbar (with WindowActionBarOverlay = true)>

    <LinearLayout paddingTop = Toolbar_height>

         *Contains all the stuff I don't want to hide*

    </LinearLayout>

</FrameLayout>

但这也不会使内容上移。因此,在我为工具栏设置动画后,我尝试将 LinearLayout 的顶部填充设置为 0,但这是即时的,而不是使用工具栏设置动画。因此,我尝试使用 animate().translateY() 为整个 LinearLayout 设置动画,但这有点滞后并且有一些不需要的副作用。

有人有什么想法吗?对于 RecyclerView,最好 minSDK 为 15。

【问题讨论】:

    标签: android android-recyclerview android-toolbar


    【解决方案1】:

    尝试在工具栏的翻译上添加一个动画监听器来更新填充。餐巾纸背面代码:

    toolbar.animate()
        .translateY(-toolbar.getHeight())
        .setUpdateListener(new AnimatorUpdateListener()) {
    
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                contentView.setPadding(
                    contentView.getPaddingLeft(),
                    // The padding is the inverse of the animation progress.
                    toolbar.getHeight() * (1f - animation.getAnimatedFraction()),
                    contentView.getPaddingRight(),
                    contentView.getPaddingBottom());
            }
    
        });
    

    我很想看看像这样在每个动画帧上更新布局的性能如何。

    【讨论】:

    • 不幸的是,有明显的延迟,类似于我尝试为整个内容设置动画时向上查看。
    • 用动画来代替边距如何?它有点更多样板(getLayoutParams,然后对其进行修改,并调用setLayoutParams),但值得一试。
    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多