【问题标题】:Toolbar animation with ViewPropertyAnimator doesn't work second time带有 ViewPropertyAnimator 的工具栏动画第二次不起作用
【发布时间】:2016-11-12 05:35:56
【问题描述】:

第一次工具栏动画效果很好,工具栏动画成功退出屏幕。

问题:工具栏上的动画在点击任何视图时不起作用,工具栏不会通过动画重新出现在屏幕上。

活动的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_image_viewer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/activity_image_viewer_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" />

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

</RelativeLayout>

工具栏代码是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

</android.support.v7.widget.Toolbar>

活动代码:在onCreate中,这会隐藏状态栏、导航栏和工具栏

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
    layoutParams.setMargins(0, uiUtils.getStatusBarHeight(getResources()), 0, 0);
    toolbar.setLayoutParams(layoutParams);

toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
            }
            uiUtils.hideStatusNavigationBar(getWindow());

            ViewTreeObserver observer = toolbar.getViewTreeObserver();
            if (observer.isAlive()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    observer.removeOnGlobalLayoutListener(this);
                } else {
                    observer.removeGlobalOnLayoutListener(this);
                }
            }
        }
    });

以下是有问题的代码,工具栏不会通过动画重新出现在点击任何视图时(OnClickListener 已经设置在视图上)

@Override
public void onClick(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
    }
    uiUtils.showStatusNavigationBar(getWindow());
}

【问题讨论】:

  • 你必须先隐藏工具栏。比如:toolbar.animate().translationY(-toolbar.getHeight()).start();
  • 对不起兄弟,我已经更新了那行代码。我已经改变了它以测试不同的标准。

标签: android toolbar uiviewanimation viewanimator viewpropertyanimator


【解决方案1】:

我错误地在 ViewPager 上设置了 OnClickListener,现在我意识到 onClick() 方法永远不会为视图寻呼机调用。 这就是工具栏重新出现动画不起作用的原因。

有时我们并不介意非常简单的事情。

解决方案:从 ViewPager 中移除 OnClickListener 并将其设置为其他可点击视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多