Skip to content

Instantly share code, notes, and snippets.

@lopspower
Last active September 30, 2023 10:50
Show Gist options
  • Save lopspower/57864633285d8695603b84c8b680df5b to your computer and use it in GitHub Desktop.
Save lopspower/57864633285d8695603b84c8b680df5b to your computer and use it in GitHub Desktop.
RecyclerView Adapter Kotlin Android Studio Template
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
class ${NAME} : RecyclerView.Adapter<${Model}Adapter.ViewHolder>() {
var data: MutableList<${Model}> = mutableListOf()
set(value) {
field = value
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.${Item_Layout_ID}, parent, false))
override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(data[position])
override fun getItemCount() = data.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(item: ${Model}) = with(itemView) {
// TODO: Bind the data with View
}
}
}
@rcd27
Copy link

rcd27 commented Dec 6, 2019

Highly recommend you to check out one good lib for reducing this boilerplate in your sources: https://github.com/sockeqwe/AdapterDelegates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment