【发布时间】:2023-02-17 23:55:24
【问题描述】:
我有一个 recyclerview,它加载了一张卡片列表,每张卡片都有一个最喜欢的按钮。当用户触摸该按钮时,我将列表项设置的顺序更改为首先放置新的收藏卡。该更改在调用 NotifyItemMoved 时显示默认动画,但我想在前面显示向上移动的项目。默认动画显示正在移动到 botton 的项目在其余项目的前面。
搜索了一下,我发现我可以实现一个自定义的 ItemAnimator 并使用这样的东西:
override fun animateMove(
holder: RecyclerView.ViewHolder?,
fromX: Int,
fromY: Int,
toX: Int,
toY: Int
): Boolean {
if ( fromY > toY) {
holder?.itemView?.bringToFront()
}
return super.animateMove(holder, fromX, fromY, toX, toY)
}
看起来效果很好,但是当我滚动列表时它崩溃并出现此错误:java.lang.RuntimeException: trying to unhide a view that was not hiddenandroidx.constraintlayout.widget.ConstraintLayout
如果我删除holder?.itemView?.bringToFront(),默认动画会再次正常运行而不会发生任何崩溃
【问题讨论】:
标签: android kotlin android-recyclerview