You may already have used a RecyclerView before with kotlin extensions enabled. It used to be fun, and now the technology has headed bit further. Am gonna teach you the most recent and cleaner way to make a recyclerView adapter as you will be using ViewBinding.
In the course of me writing a couple of articles, I'll be referencing most stuff from a dummy app i published on google play. I called it Wakanda Forever Wallpapers. It's some kind of a gallery app comprising of a set photos i collected from the web. So in there i have implemented a RecyclerView with ViewBnding, an Options Menu as well plus so much more educative stuff you might wanna get your eyes on.
Getting started with the making of our Adapter, you gotta make sure a list of items you want to display on your RecyclerView is at hand plus i assume you have a complete idea on how you deal with the RecyclerView widget and have created an item layout for it as well. So in my case i had a list of images on my drawable folder and so created an object and stashed my images in there.
object IsiyoBadilika {
fun feedYaPichaProvider(): ArrayList {
val feedYaPicha: ArrayList = ArrayList()
feedYaPicha.add(R.drawable.pre1)
feedYaPicha.add(R.drawable.pre2)
feedYaPicha.add(R.drawable.pre3)
feedYaPicha.add(R.drawable.pre4)
feedYaPicha.add(R.drawable.pre5)
feedYaPicha.add(R.drawable.pre6)
feedYaPicha.add(R.drawable.pre7)
feedYaPicha.add(R.drawable.pre8)
feedYaPicha.add(R.drawable.pre10)
return feedYaPicha
}
} So that's a sample from my list of items am planning to display on the RecyclerView. As you can see, it's an array list of images. Yours could be strings and stuff. It depends on your app design, so don't you worry about slight differences. A sample of my widget is also down below so you can take a look at it, just to have a wider perspective of what am trying to do right now.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rez"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/individual_photo_design"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"/ >
Again that's my widget right there. Make yours available as well. Having all that all setup we should be ready to kickoff with preparing our adapter alongside View Binding. Let's begin with step one.
1. Create a class for our Adapter
The process should not be terrifying, because the class am saying you have to create should just be an ordinary class but only the naming. Try and make the name of your class as relevant as it can be, because it's for our adapter class
class AdapterYaWakanda(private val mahalia: Context, private val feedYaPicha: ArrayList<Tnt>): RecyclerView.Adapter<AdapterYaWakanda.AdapterForever>() {
}
The name of my adapter class is what i can say relevant to my project, so make yours too. I passed in a Context, and a list of images i showed you a couple of paragraphs ago. The most important part you wanna have i just passing in the list, worry less about my context cos i had some uses with it that you may or may not have. As you may have seen, our adapter class inherits from RecyclerView.Adapter within it we can be able to see that the name of our class is passed but only we see something strange on dot front of it. What is that?. Well it's an inner class we have not created yet, so it's something we yet have to make next.
2 Make an inner class
inner class AdapterForever(private val kiunganishi: IndividualPhotoDesignBinding): RecyclerView.ViewHolder(kiunganishi.root){
fun ninaBind(picha: Int){
kiunganishi.picha.setImageResource(picha)
kiunganishi.picha.setOnClickListener {
mahalia.startActivity(Intent(mahalia, MainActivity2::class.java)
.putExtra("picha", picha))
}
}
}
Backing up what you are seeing right now, t's the class i have been talking about, only that i have passed some-kind of a parameter kiunganishi of type ?, well it's of the type your item layout binding. My item layout i called IndividualPhotoDesign, so find a more cooler name for your item layout and it has to look like CoolerNameBnding. Again the class has to inherit from RecyclerView but this times it's dot viewholder() which will require a view.. Luckily we got a bind to our view in place already, it's kiunganishi.root. The function you see in there is just a custom function. You create one of your own and pass in an individual data you want to appear on each item of your RecyclerView. My image resources in drawable folder are kept as integers and so i pass the images as some-kind of ints.
The kiunganishi allows us to access views from the item layout and so we can bind the data to these. To this point you now clearly understand what the function does in there. that's good, so let's keep moving..
3. getItemCount
override fun getItemCount(): Int {just a reminder for all of us who might have forgotten. The .size is used there to determine the size of the array i just passed. So whatever the size of your list, it's always going to return the number of items that are available in there.
return feedYaPicha.size
}
4. Override onCreateViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterForever {
return AdapterForever(IndividualPhotoDesignBinding
.inflate(LayoutInflater.from(parent.context), parent, false))
}
5. Override onBindViewHolder
override fun onBindViewHolder(holder: AdapterForever, position: Int) {
holder.ninaBind(feedYaPicha[position])
}
0 Comments