Last active
March 3, 2022 14:24
-
-
Save sumeetph/178891c67afc4a73df8561a745f4108e to your computer and use it in GitHub Desktop.
Cache OnChanged callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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