我在循环视图中的一些按钮项目上添加了点击动画。
一切都很顺利,但是当我向下滚动列表时,一些按钮会出现一种奇怪的状态,就像它们在动画阶段被冻结一样,即使这些按钮没有触发动画,而对于其他按钮,它们的视图设置为不可见/消失。
这是收缩动画代码的一部分,其中shrink是一个ValueAnimator对象。
shrink.addUpdateListener { animation ->
val animatedValue = animation.animatedValue as Int
v.layoutParams.width = animatedValue
v.requestLayout()
}
shrink.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
super.onAnimationStart(animation)
v.visibility = View.VISIBLE
v.isEnabled = false
}
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
v.layoutParams.width = 0
v.visibility = View.GONE
v.isEnabled = false
}
}
我有一个View
和ViewHolder
类,在这个类中,我在滚动时绑定了循环视图项的可见性。
在动画生命周期中,我是否遗漏了与回收视图项一起工作的步骤?我在想,也许我必须处理onAnimationStop
或onAnimationPause
的情况,但我不确定。
任何帮助都将不胜感激。
发布于 2019-12-24 03:51:35
您可以使用setHasTransientState
将视图标记为一种“脏”,这样RecyclerView就不会在动画未完成时重用它。这应该可以解决您的问题。修改后的代码如下:
shrink.addUpdateListener { animation ->
val animatedValue = animation.animatedValue as Int
v.layoutParams.width = animatedValue
v.requestLayout()
}
shrink.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
super.onAnimationStart(animation)
v.setHasTransientState(true) // Mark view
v.visibility = View.VISIBLE
v.isEnabled = false
}
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
v.layoutParams.width = 0
v.visibility = View.GONE
v.isEnabled = false
v.setHasTransientState(true) // "Return" it to the pool
}
}
这正是PropertyAnimation在幕后做的事情。希望这能有所帮助:)
发布于 2018-06-19 12:34:50
首先删除默认的RecyclerView
项动画制作程序
recyclerView.setItemAnimator(null);
并且还取消适配器onViewRecycled
中的任何自定义运行动画
https://stackoverflow.com/questions/50363254
复制相似问题