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
| //Return cached view holder for particular position | |
| private val viewCacheExtension = object : ViewCacheExtension() { | |
| override fun getViewForPositionAndType( | |
| recycler: Recycler, | |
| position: Int, | |
| type: Int | |
| ): View? { | |
| val cachedItemId = getItemId(position) | |
| if (cachedViewHolderTypes.contains(type) && cachedItemId != NO_CACHED_ITEM_ID | |
| && cachedItems.containsKey(cachedItemId) |
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
| //Saving the cached item once it goes out of screen | |
| override fun onViewDetachedFromWindow(holder: ViewHolder) { | |
| if (cachedViewHolderTypes.contains(holder.itemViewType)) { | |
| val pos = holder.bindingAdapterPosition | |
| Timber.d("onViewDetached called for position : $pos") | |
| if (pos > -1) { | |
| holder.setIsRecyclable(false) | |
| cachedItems[getCachedItemId(pos)] = holder | |
| } |
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) { |
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 onItemRangeRemoved(positionStart: Int, itemCount: Int) { | |
| Timber.d("On Item range removed called for adapter observer") | |
| if (cachedItems.isEmpty()) | |
| return | |
| val iterator: MutableIterator<String> = cachedItems.keys.iterator() | |
| while (iterator.hasNext()) { | |
| val cachedItemId = iterator.next() | |
| val index = cachedItems[cachedItemId]?.bindingAdapterPosition ?: -1 | |
| if (index != -1 && positionStart <= index && index < positionStart + itemCount) { |
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
| import android.view.View | |
| import androidx.recyclerview.widget.RecyclerView | |
| import androidx.recyclerview.widget.RecyclerView.* | |
| import timber.log.Timber | |
| import java.util.HashSet | |
| import java.util.concurrent.ConcurrentHashMap | |
| /** | |
| * Created by Sumeet on 24,January,2022 |