fun apply(recyclerView: RecyclerView, items: Iterable<Item>?) { if (items != null) { val cache: HashMap<Int, Stack<RecyclerView.ViewHolder>> = HashMap() for (d in items) { if (!cache.containsKey(d.getType())) { cache[d.getType()] = Stack<RecyclerView.ViewHolder>() } if (mCacheSize === -1 || cache[d.getType()].size() <= mCacheSize) { cache[d.getType()].push(d.getViewHolder(recyclerView)) } val recyclerViewPool = RecycledViewPool() for ((key, value): Map.Entry<Int?, Stack<RecyclerView.ViewHolder?>> in cache.entrySet()) { recyclerViewPool.setMaxRecycledViews(key!!, mCacheSize) for (holder in value) { recyclerViewPool.putRecycledView(holder) } value.clear() } cache.clear() recyclerView.setRecycledViewPool(recyclerViewPool) } } }
init the cache on your own .