Created
October 30, 2018 10:03
-
-
Save bitvale/28354d822e1389053a864816e2e705b1 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
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