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