In every single android application, we need to make many number of recycler-view adapter. So every time you create a blank adapter and modify it as per your requirements. But it is very boring and as well as time taking. So here I give you a solution that you can create any recyclerview adapter within 5 seconds using file template in Android Studio.
So, Lets read this full article-
First we open android studio and create a project. After project creation
Open File->new->Edit File Template...
Follow this below screenshot
Okay, After click on "Edit File Template" a popup is open "File and Code Template", Now click on the
"+" icon and set set your template name.
Now set your template name, template extension and template body. after doing everything click on "Apply" and then click on "OK"
The template body I after after the screenshot
Template Body
Copy this code and paste it on the template body section, which I marked in screenshot.
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME} import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView #parse("File Header.java") class $AdapterName(private val list: ArrayList<$Model>) : RecyclerView.Adapter<$AdapterName .ViewHolder>() { //this method is returning the view for each item in the list override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val v = LayoutInflater.from(parent.context) .inflate(R.layout.$xml_view, parent, false) return ViewHolder(v) } //this method is binding the data on the list override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.bindItems(list[position]) } //this method is giving the size of the list override fun getItemCount(): Int { return list.size } //the class is hodling the list view class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bindItems(item: $Model) { #end } } }
Now, close the pop-up and click on File->new and check the list you find your created template in the list. For my case, I set the template name "RecyclerViewAdadpter". So I see this and click on this a pop-up is open.
After open the pop-up fill up all the field with appropriate text and simply click on OK. And you can see a ready adapter is created in your project. Now every time you can use this template to create recyclerview with different name, different xml view and different Object, Model class etc. For my case I just take "String", you can take any object or model class, as you wish.
It is the adapter create using template
For better Understanding see the YouTube video below I mentioned-
If you think it is helpful, please share with your android developer community.
Nice blog, it is very impressive.
ReplyDeleteKotlin Training in Chennai
Kotlin Online Training
Kotlin Course in Bangalore
Thanks for your feedback. Please share and support me
Delete