RecyclerView 不会在项目最初出现时对其进行动画处理,只有在它再次被回收时才进行动画处理

RecyclerView doesn't animate an item when it initially appears, only when it is recycled again

我有一个 TranslateAnimation 用于在 RecyclerView.Adapter 的项目中查看。动画最初出现时应应用于特定的列表项,但它仅在您向后滚动并再次回收该项目时起作用。

我的猜测是它与 RecyclerView 的生命周期有关,但我无法弄清楚是什么导致动画无法启动。

class mAdapter(items: List<String>): RecyclerView.Adapter<mAdapter.ViewHolder>(){

  private var mPosition = 0

  // The animation will be applied to the first item
  override fun getItemViewType(position: Int): Int {
    if (position == mPosition){
      return 1
    } else {
      return 0
    }
  }

  override fun onViewRecycled(holder: ViewHolder) {
        super.onViewRecycled(holder)

        // Animate the view inside the item
        if (holder.itemViewType == 1){
            holder.animateItem()
        }
    }

  override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)

        // Animate the view inside the item
        if (holder.itemViewType == 1){
          holder.animateItem()
        }
    }

  inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
    val picture: ImageView? = view.findViewById(R.id.picture)       

    fun animateItem(){
      val itemWidth: Float = itemView.width.toFloat()
      val animator = TranslateAnimation(-itemWidth, 0f, 0f, 0f)

      animator.repeatCount = 0
      animator.interpolator = AccelerateInterpolator(1.0f)
      animator.duration = 700
      animator.fillAfter = true

      background?.animation = animator
      background?.startAnimation(animator)
    }  

  }

}

如果我在 animateItem 中记录一条消息,它将在 RecycleView 加载时出现,但在我向下和向上滚动之前不会显示动画。

回答

正如NSimon指出的那样,解决方案是写一个addOnGlobalLayoutListener

override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
  holder.itemView.viewTreeObserver.addOnGlobalLayoutListener{
    holder.animateItem()
  }
}

你是对的。 ViewHolder 仅在首次使用后的某个时间被回收。不要在 onViewRecycled 中制作动画。您可以试试 onBindViewHolder,尽管我不确定您要查找的确切时间。

正如评论中所讨论的,您的第一个动画实际上可以正常播放。 但是,第一次调用它时,View 并未在屏幕上完全分层。因此,itemView.width.toFloat() 返回的是 0,而您的动画是从 0 到 0。

快速的解决方案是将动画的启动封装在 GlobalLayoutListener 中(这是系统告诉您视图已分层)。

像这样:

override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
  holder.itemView.viewTreeObserver.addOnGlobalLayoutListener{
    holder.animateItem()
  }
}

不过请记住,您应该在启动动画后删除 globalLayoutListener(否则它会永远留在那里并会继续触发 if/when 视图发生某些事情)。所以更好的方法是创建一个像这样的辅助函数:

inline fun <T: View> T.afterMeasured(crossinline f: T.() -> Unit) {
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {    
        override fun onGlobalLayout() {    
            if (measuredWidth > 0 && measuredHeight > 0) {    
                viewTreeObserver.removeOnGlobalLayoutListener(this)    
                f()    
            }    
        }    
    })   
}

并像这样在 onBindViewHolder 中调用它:

override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
  holder.itemView.afterMeasured{
    holder.animateItem()
  }
}