带有 ViewPropertyAnimator 的工具栏动画第二次不起作用
Toolbar animation with ViewPropertyAnimator doesn't work second time
第一次工具栏动画效果很好,工具栏动画成功离开屏幕。
问题:点击任何视图时工具栏上的动画不起作用,工具栏不会通过动画重新出现在屏幕上。
xml 代码 activity:
<?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>
Activity代码:在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);
}
}
}
});
下面是有问题的代码,Toolbar 不会通过动画重新出现 点击任何视图(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());
}
我错误地在 ViewPager 上设置了 OnClickListener,现在我意识到永远不会为视图寻呼机调用 onClick() 方法。
这就是工具栏重现动画不起作用的原因。
有时候我们并不介意很简单的事情。
解决方法:将ViewPager中的OnClickListener去掉,设置为其他可点击视图。
第一次工具栏动画效果很好,工具栏动画成功离开屏幕。
问题:点击任何视图时工具栏上的动画不起作用,工具栏不会通过动画重新出现在屏幕上。
xml 代码 activity:
<?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>
Activity代码:在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);
}
}
}
});
下面是有问题的代码,Toolbar 不会通过动画重新出现 点击任何视图(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());
}
我错误地在 ViewPager 上设置了 OnClickListener,现在我意识到永远不会为视图寻呼机调用 onClick() 方法。 这就是工具栏重现动画不起作用的原因。
有时候我们并不介意很简单的事情。
解决方法:将ViewPager中的OnClickListener去掉,设置为其他可点击视图。