Skip to content

Instantly share code, notes, and snippets.

@sumeetph
Last active March 3, 2022 14:24
Show Gist options
  • Select an option

  • Save sumeetph/178891c67afc4a73df8561a745f4108e to your computer and use it in GitHub Desktop.

Select an option

Save sumeetph/178891c67afc4a73df8561a745f4108e to your computer and use it in GitHub Desktop.
Cache OnChanged callback
override fun onChanged() {
Timber.d("items added or moved")
if (cachedItems.isEmpty())
return
val iterator: MutableIterator<String> = cachedItems.keys.iterator()
val newItemsIds = mutableSetOf<String>()
for (newIndex in 0 until itemCount) {
val itemId = getItemId(newIndex)
newItemsIds.add(itemId)
}
//Remove cached items which are not in new set
while (iterator.hasNext()) {
val cachedItemId = iterator.next()
val holder = cachedItems[cachedItemId]
if (holder?.bindingAdapterPosition == -1 || !newItemsIds.contains(cachedItemId)) {
holder?.itemView?.let { view ->
if (view.isAttachedToWindow || view.parent != null) {
recyclerView?.removeView(view)
view.visibility = View.GONE
}
}
iterator.remove()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment