问题描述
我有复杂和通用的 RecyclerView 设计和列表适配器。
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): BaseViewHolder {
val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding =
DataBindingUtil.inflate(layoutInflater,viewType,parent,false)
return object : BaseViewHolder(binding) {
override fun bindData(position: Int) {
val model = getItem(position).data
itembinding.setvariable(BR.model,model)
viewmodel?.let {
itembinding.setvariable(BR.viewmodel,it)
}
}
}
}
override fun onBindViewHolder(holder: BaseViewHolder,position: Int) {
holder.run {
bindData(position)
if (itembinding.hasPendingBindings()) {
itembinding.executePendingBindings()
}
}
}
它在 RecyclerView 中将 RecyclerView 作为项目并自行处理多布局。我使用数据绑定适配器更新列表和项目。当我需要更新单个项目时;我在 LiveData 列表中搜索所有树,修改值并将值更新列表再次发布到 LiveData。
当每个视图的值发生变化时,我想用动画(RecyclerView 内的 RecyclerView 内的项目)更新每个视图。
这是我的更新代码;
@BindingAdapter("setTransactionBgAnimation")
fun View.setTransactionBgAnimation(ratio: Double?) {
ratio?.let { value ->
val colorAnim = ObjectAnimator.ofInt(
this,"backgroundColor",getEvaluateColor(context,value),Color.WHITE
)
colorAnim.duration = 500
colorAnim.repeatCount = 1
colorAnim.start()
val alphaAnim = ObjectAnimator.ofFloat(
this,"alpha",0.40f,0.0f
)
alphaAnim.duration = 500
alphaAnim.repeatCount = 1
alphaAnim.start()
}
}
值更新时;它从所有视图中调用了每次更改。 我试图在绑定适配器中提供唯一的标签来查看和检查标签,但它对我不起作用。
解决方法
我用不那么干净的方式解决了这个问题。
首先;为每行的每个可见项目的计数调用动画,我通过控制给视图 tag
更改值并检查与新值相同的标签来修复它。
第一次修复后,只有真正改变项目动画但它动画多次。这是由于 ObjectAnimator
的 backgroundColor
动画造成的。我不知道为什么我什至用动画改变了背景颜色。我删除了它并修复了多个闪烁的动画。
为了更好的理解,请看我的代码部分
fun View.setTransactionBgAnimation(ratio: Double?) {
if (tag != ratio.toString()) {
ratio?.let { value ->
setBackgroundColor(getEvaluateColor(context,value))
val alphaAnim = ObjectAnimator.ofFloat(
this,"alpha",0.40f,0.0f
)
alphaAnim.duration = 500
alphaAnim.start()
}
tag = ratio.toString()
}
}