本文介绍了有一个按钮上的涟漪动画的最终执行OnClickListener(材料主题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察在Android的一个按钮,波纹动画的错误。基本上,我有一个ViewGroup中,称之为ButtonContainer包含2个按钮,叫他们OkButton和CancelButton。当用户点击CancelButton,我想通过设置它的知名度去躲ButtonContainer。但是,这是造成副作用,那就CancelButton纹波动画排队,但从来没玩过,它起到下一次ButtonContainer变得可见。这似乎发生,因为纹波动画有机会发挥之前按钮的OnClickListener被触发。本文介绍了此错误确切,并显示该视图究竟是如何表现:

I'm observing a bug with ripple animation on a button in Android. Basically, I have a ViewGroup, call it ButtonContainer that contains 2 buttons, call them OkButton and CancelButton. When the user clicks on CancelButton, I want to hide ButtonContainer by setting it's visibility to GONE. But this is causing a side effect, that the ripple animation on CancelButton is queued, but never played, and it plays the next time ButtonContainer becomes visible. This seems to happen because button's OnClickListener is fired before the ripple animation has a chance to play. This article describes this exact bug and shows how exactly the view behaves: link

笔者提出一个解决办法,但我想知道,有没有办法有OnClickListener被解雇的之后的按钮上的纹波播放动画时?

The author suggests a workaround, but I'm wondering, is there a way to have the OnClickListener get fired after ripple animation on a button is played?

推荐答案

答案很简单:不是真的,并根据材料管理准则(据我所知),你不应该推迟在这样一个onClick的执行办法。在我看来,这将导致pretty差UX - 我喜欢我的按钮瞬间,我不喜欢等待花哨的动画

The short answer: not really and according to the Material guidelines (AFAIK) you shouldn't delay the onClick execution in such a way. In my opinion it would result in pretty poor UX - I like my buttons snappy and I don't like waiting for fancy animations.

中等长度的半答案:,你可以做一个快速的黑客和包装你的onClick逻辑的肉在。postDelayed(...)足够好的延迟调用。有了适当的延迟,该行动将纹波后发生。这是一个黑客,那是脆的,我劝你对这个解决方案。

The medium-length semi-answer: you could do a quick hack and wrap the meat of your onClick logic in a .postDelayed(...) call with a good enough delay. With a proper delay, the action would happen after the ripple. It is a hack, it is brittle and I would advise you against this solution.

长的答案:实际上你可以决定是否纹波已完成动画,虽然这路径是有点复杂。纹波是一个可绘制,并且所有可绘制动画,它讲述了查看时,做动画(以及何时完成它)以回调。你可以进入这个回调告诉时的动画效果实际完成。

The long answer: you can actually determine if the Ripple has finished animating, though that path is a bit complicated. Ripple is a Drawable, and as all animating Drawables, it tells its View when to do animation (and when to finish it) with a callback. You could tap into this callback to tell when the animation actually finished.

例如。你可以实现自己的回调这一点,所有呼叫转发到纹波的托管ViewGroup中,也告诉你,当动画完成这样你就可以做生意了。

E.g. you could implement your own callback for this that forwards all calls to the Ripple's hosting ViewGroup and also tell you when the animation has finished so you can do business.

至于底线,我认为你应该:

As the bottom line, I think that you should either:


  • 的文章并您联系解决这个问题

  • 忘掉在这种情况下产生的连锁反应,如果这是一个选项。

这篇关于有一个按钮上的涟漪动画的最终执行OnClickListener(材料主题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:07