Skip to content

Instantly share code, notes, and snippets.

@auras
Last active May 28, 2018 14:48
Show Gist options
  • Select an option

  • Save auras/3b67312f7f39108849107f79b473faff to your computer and use it in GitHub Desktop.

Select an option

Save auras/3b67312f7f39108849107f79b473faff to your computer and use it in GitHub Desktop.
How to disable the change animation for a specific item in RecylerView
package com.bonial.kaufda.shoppinglist
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.RecyclerView
import com.bonial.kaufda.shoppinglist.adapter.OfferSuggestionListAdapterDelegate
open class ShoppingListItemAnimator : DefaultItemAnimator() {
private val pendingRemovals = mutableListOf<RecyclerView.ViewHolder>()
// We disable the change animation
// Disabling change animations will trigger a remove and add animation
override fun animateChange(oldHolder: RecyclerView.ViewHolder, newHolder: RecyclerView.ViewHolder, fromX: Int, fromY: Int, toX: Int, toY: Int): Boolean {
if (oldHolder is OfferSuggestionListAdapterDelegate.ViewHolder && newHolder is OfferSuggestionListAdapterDelegate.ViewHolder) {
return false
}
return super.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY)
}
// We disable the remove animation
override fun animateRemove(holder: RecyclerView.ViewHolder): Boolean {
if (holder is OfferSuggestionListAdapterDelegate.ViewHolder) {
pendingRemovals.add(holder)
return false
}
return super.animateRemove(holder)
}
override fun onAddFinished(item: RecyclerView.ViewHolder) {
// After the add animation is finished we hide the old viewholder
// If we don't then it will be kept in the background and can be visible when scrolling
if (item is OfferSuggestionListAdapterDelegate.ViewHolder) {
pendingRemovals.apply {
forEach {
it.itemView.alpha = 0f
}
clear()
}
}
super.onAddFinished(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment