Skip to content

Instantly share code, notes, and snippets.

@bitvale
Created October 30, 2018 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitvale/28354d822e1389053a864816e2e705b1 to your computer and use it in GitHub Desktop.
Save bitvale/28354d822e1389053a864816e2e705b1 to your computer and use it in GitHub Desktop.
abstract class BaseAdapter<T, VH : BaseViewHolder<T>> : RecyclerView.Adapter<VH>() {
abstract var dataSet: ArrayList<T>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val view = parent.inflate(viewType)
return createViewHolder(view)
}
override fun onBindViewHolder(viewHolder: VH, position: Int) {
val obj = getObjectForPosition(position)
viewHolder.bind(obj)
}
override fun getItemViewType(position: Int) = getLayoutIdForPosition(position)
override fun getItemCount() = dataSet.size
private fun getObjectForPosition(position: Int): T = dataSet[position]
abstract fun createViewHolder(view: View): VH
@LayoutRes
abstract fun getLayoutIdForPosition(position: Int): Int
open fun replaceData(data: List<T>) {
dataSet.clear()
dataSet.addAll(data)
this.notifyDataSetChanged()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment