Created
July 29, 2021 11:34
-
-
Save ch8n/69672fba8964d3c7c2cbbb41008101c3 to your computer and use it in GitHub Desktop.
This file contains 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
class LayoutManager(private val recycler: Recycler) { | |
fun getViewForPosition(pos: Int): View { | |
return recycler.getView(pos) | |
} | |
} | |
class RecyclerView( | |
private val adapter: Adapter, | |
private val recyclerPool: RecyclerPool, | |
private val viewCache: ViewCache | |
) { | |
fun getView(pos: Int): View { | |
val viewOrNull = viewCache.getOrNull(pos) | |
// view found then return view | |
if (viewOrNull != null) { | |
return viewOrNull | |
} | |
// if view not found in cache get view type | |
val viewType = adapter.getViewType(pos) | |
// from view type get viewholder from cache | |
val holderOrNull = recyclerPool.getViewHolderByType(viewType) | |
// view holder found -> bind data and return view | |
if (holderOrNull != null) { | |
adapter.bindViewHolder(holderOrNull, pos) | |
return holderOrNull.itemView | |
} | |
return TODO() | |
} | |
} | |
class ViewCache { | |
private val viewCache = mutableMapOf<Int, View>() // LRU cache | |
fun getOrNull(pos: Int): View? = viewCache.get(pos) | |
} | |
class RecyclerPool { | |
private val cache = mutableMapOf<ViewType, ViewHolder>() // LRU cache | |
fun getViewHolderByType(viewType: ViewType): ViewHolder? { | |
return cache.get(viewType) | |
} | |
} | |
abstract class Adapter { | |
abstract fun getViewType(pos: Int): ViewType | |
abstract fun bindViewHolder(holder: ViewHolder,pos:Int) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment