Skip to content

Instantly share code, notes, and snippets.

@ch8n
Created July 29, 2021 11:32
Show Gist options
  • Save ch8n/c91c1cfa6fad0826ac3842ad64e36cfe to your computer and use it in GitHub Desktop.
Save ch8n/c91c1cfa6fad0826ac3842ad64e36cfe to your computer and use it in GitHub Desktop.
// layout manager calls function getViewForPosition over recycler
// thus has a recycler.
class LayoutManager(private val recycler: Recycler) {
fun getViewForPosition(pos: Int): View {
return recycler.getView(pos)
}
}
class RecyclerView(
private val viewCache: ViewCache
) {
fun getView(pos: Int): View {
val viewOrNull = viewCache.getOrNull(pos)
// view found then return view
if (viewOrNull != null) {
return viewOrNull
}
return TODO()
}
}
class ViewCache {
private val viewCache = mutableMapOf<Int, View>() // LRU cache
fun getOrNull(pos: Int): View? = viewCache.get(pos)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment