Skip to content

Instantly share code, notes, and snippets.

@radityagumay
Last active January 15, 2018 00:59
Show Gist options
  • Save radityagumay/534827f67fab594fa147c5f171b18e5c to your computer and use it in GitHub Desktop.
Save radityagumay/534827f67fab594fa147c5f171b18e5c to your computer and use it in GitHub Desktop.
class UniversalAdapter<T, VH : RecyclerView.ViewHolder>(
private val onCreateViewHolder: (ViewGroup?, Int) -> VH,
private val onBindViewHolder: (VH, Int, T) -> Unit,
private val onViewType: ((Int) -> Int)? = null) : RecyclerView.Adapter<VH>(),
Universal<T> {
var items = mutableListOf<T>()
private set
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): VH =
onCreateViewHolder.invoke(parent, viewType)
override fun onBindViewHolder(holder: VH, position: Int) {
onBindViewHolder.invoke(holder, position, items[position])
}
override fun getItemViewType(position: Int) = onViewType?.invoke(position) ?: super.getItemViewType(position)
override fun getItemCount() = items.size
override fun addAll(items: Collection<T>) {
this.items.addAll(items)
notifyDataSetChanged()
}
override fun add(item: T) {
this.items.add(item)
notifyItemInserted(this.items.size)
}
override fun remove(item: T, position: Int) {
this.items.remove(item)
notifyItemRemoved(position)
}
override fun removeRange(vararg items: T) {
items.forEachIndexed { index, item -> remove(item, index) }
}
override fun update(item: T) {
items.forEachIndexed { index, i ->
if (i == item) {
items[index] = item
notifyItemChanged(index)
}
}
}
override fun updateRange(vararg items: T) {
for (i in 0 until this.items.size) {
(0 until items.size)
.filter { i == it }
.forEach {
this.items[i] = items[it]
update(this.items[i])
}
}
}
override fun clearAndAddAll(collection: Collection<T>) {
items.clear()
addAll(collection)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment