Skip to content

Instantly share code, notes, and snippets.

@bitvale
Last active October 30, 2018 10:04
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/08d163baf36f833b5f7f14bc46e860bc to your computer and use it in GitHub Desktop.
Save bitvale/08d163baf36f833b5f7f14bc46e860bc to your computer and use it in GitHub Desktop.
abstract class BaseRecyclerAdapter<T, VH : BaseViewHolder<T>> : RecyclerView.Adapter<VH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val inflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding = DataBindingUtil.inflate(inflater, viewType, parent, false)
return createViewHolder(binding)
}
override fun onBindViewHolder(holder: VH, position: Int) {
val obj = getObjectForPosition(position)
holder.bind(obj)
}
override fun getItemViewType(position: Int) = getLayoutIdForPosition(position)
abstract fun getObjectForPosition(position: Int): T
abstract fun createViewHolder(binding: ViewDataBinding): VH
@LayoutRes
abstract fun getLayoutIdForPosition(position: Int): Int
abstract fun replaceData(data: List<T>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment