Kotlin File Template for RecyclerView Adapter

Android studio is extremely powerful with a lot of tricks under it’s sleeves which are waiting to be explored. File template is one of them. What is file template? A file template is a source that already contains some code to get you started.

In this article, we are going to create a file template for RecyclerView Adapter implementation which is something I often have to look up when trying to create it.

Steps to add RecyclerView Adapter file template

Press Ctrl+Shift+A (windows) or Cmd+Shift+A (Mac) to open action window and type “Add Template”
1_EUSbLklAIF2sCUwObXPfng
Click “Plus icon” (+)
1_UDtF-04_vYRwYOPJ6IpYPQ
Give some name to the template (I have given RecyclverView Adapter) and make sure you have given the “kt” Extension

Then we will be able to write your template code in the code section (below the name). There are couple of variables which we can use to refer file name, package name, date and many more. Some of them are

${NAME} is the file name that we have given at the time of file creation

${PACKAGE_NAME} is the package name

${DATE} the current date of system

${PROJECT_NAME} is the name of current project

${USER} is currently user system login name

We can also define any custom variable that we will assign at the time of creation of our custom file. Here we will use ${Model_Class} to refer the model class and ${Item_Layout_ID} to refer the XML file for view holder

Now paste the following code and click “Ok”

#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.*
import org.jetbrains.anko.sdk25.coroutines.onClick

#parse("File Header.java")
class ${NAME}(var items:ArrayList<${Model_Class}>):RecyclerView.Adapter<${NAME}.${NAME}ViewHolder>(){
    var listener:${NAME}Listener?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):${NAME}ViewHolder{
        return ${NAME}ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.${Item_Layout_ID}, parent, false))
    }
    
    override fun getItemCount() = items.size
    
    override fun onBindViewHolder(holder: ${NAME}ViewHolder, position: Int){
        listener?.let{ holder.bind(it, items[position])}
    }
    
    interface ${NAME}Listener{
        fun OnItemClick(model:${Model_Class})
    }

    class ${NAME}ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        
        fun bind(listener:${NAME}Listener, model:${Model_Class}){
            itemView.onClick{
                listener.OnItemClick(model)
            }
        }
    }
}

Here I have created an inner class for the view holder and an interface to get the callback of item click.

Create RecyclerView Adapter

Now our RecyclerView Adapter file template is ready to use. Open new file dialog and we will see the file template (for me it is “RecyclerView Adapter”)

1_TPOU6Zlaqxgr7l6Ha6dxWA

Just enter all the details

1_0Ml_Hs9LIgu8OZ4dSh_fCA

That’s it. Now we can get ride of writing all the boilerplate code again and again. Happy Coding.