Skip to content

Instantly share code, notes, and snippets.

@gsanthosh91
Created June 18, 2018 05:41
Show Gist options
  • Select an option

  • Save gsanthosh91/4f68942758a59864ae69f1edababd990 to your computer and use it in GitHub Desktop.

Select an option

Save gsanthosh91/4f68942758a59864ae69f1edababd990 to your computer and use it in GitHub Desktop.
Live template for Recyclerview Adapter in Kotlin
#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
class ${NAME}(private val list: List<${LIST_MODEL}>) : RecyclerView.Adapter<${NAME}.CustomViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val rowView = LayoutInflater.from(parent.context).inflate(R.layout.${item_layout}, parent, false)
return CustomViewHolder(rowView)
}
override fun getItemCount(): Int {
return list.count()
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val item = list.get(position)
//holder.rowView.name.text = item.name
holder.item = item
}
class CustomViewHolder(val rowView: View, var item: ${LIST_MODEL}? = null) : RecyclerView.ViewHolder(rowView) {
init {
rowView.setOnClickListener {
//println(item)
}
}
}
}
@gsanthosh91
Copy link
Author

HOW TO USE

Move this file into template directory

Windows: <your_user_home_directory>\.AndroidStudio<version_number>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates

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